安卓应用中的图片查看器

4

我想打开一个存在于我的应用程序目录中,使用内置的Android图片查看器写入的图像。这个图像是在应用程序目录的不同部分中被写入的。当获取以下文件时:

super.getFilesDir() + "/current.png"

File.exists()返回true。

我该如何打开内置的Android图片查看器来查看这个文件? 目前我正在做:

File f = new File(super.getFilesDir()+"/current.png");
uri = Uri.parse("file://"+super.getFilesDir()+"/current.png");
startActivity(new Intent(Intent.ACTION_VIEW, uri));

并且它不断地输出:

10-11 13:09:24.367: INFO/ActivityManager(564): 正在启动 活动: Intent { act=android.intent.action.VIEW dat=file:///data/data/com.davidgoemans.myapp/files/current.png } 10-11 13:09:24.367: ERROR/myapp(2166): 异常发生android.content.ActivityNotFoundException: 找不到处理 Intent 的活动 { act=android.intent.action.VIEW dat=file:///data/data/com.davidgoemans.myapp/files/current.png }

无论我将 uri schema 更改为什么(例如 content://、file://、media://、image://),都是如此。

3个回答

6
一种方法是实现上下文提供程序,以使其他应用程序可以访问您的数据。
创建一个包含以下内容的新类:
public class FileContentProvider extends ContentProvider {
   private static final String URI_PREFIX = "content://uk.co.ashtonbrsc.examplefilecontentprovider";

   public static String constructUri(String url) {
       Uri uri = Uri.parse(url);
       return uri.isAbsolute() ? url : URI_PREFIX + url;
   }

   @Override
   public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
       File file = new File(uri.getPath());
       ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
       return parcel;
   }

   @Override
   public boolean onCreate() {
       return true;
   }

   @Override
   public int delete(Uri uri, String s, String[] as) {
       throw new UnsupportedOperationException("Not supported by this provider");
   }

   @Override
   public String getType(Uri uri) {
   throw new UnsupportedOperationException("Not supported by this provider");
   }

   @Override
   public Uri insert(Uri uri, ContentValues contentvalues) {
       throw new UnsupportedOperationException("Not supported by this provider");
   }

   @Override
   public Cursor query(Uri uri, String[] as, String s, String[] as1, String s1) {
       throw new UnsupportedOperationException("Not supported by this provider");
   }

   @Override
   public int update(Uri uri, ContentValues contentvalues, String s, String[] as) {
       throw new UnsupportedOperationException("Not supported by this provider");
   }

}

将内容提供者添加到您的AndroidManifest.xml文件中:
<provider android:name=".FileContentProvider" android:authorities="uk.co.ashtonbrsc.examplefilecontentprovider" />

然后,您应该能够在ACTION_VIEW意图中使用"content://uk.co.ashtonbrsc.examplefilecontentprovider/" + 图像的完整路径

谢谢,今晚会尝试一下。我基本上已经实现了那个,只是似乎执行一个应用程序太过于繁琐了。设计/架构很棒,但对于基本使用来说过于复杂了。 - DavidG
注意:重写getType()方法需要设置MIME类型,例如: @Override public String getType(Uri uri) { return "image/png"; } - DavidG
这在我的应用程序中运行良好,感谢你们两个...我很惊讶很少有应用程序可以使用我的意图。Intent.ACTION_VIEW是错误的选择吗?我应该选择除image/jpg之外的其他mime类型吗? - jettero

3
选项#1:创建一个ContentProvider来提供位于应用程序私有文件区域中的文件,然后使用ACTION_VIEW Intent在该content:// Uri上操作。
选项#2:将文件移动到SD卡,并在Uri上使用ACTION_VIEW Intent,同时加上适当的MIME类型。Android不会自动将文件扩展名与MIME类型关联起来,因此您需要告诉Intent这个Uri指向什么类型的MIME类型。这对于您来说是“自动”处理的ContentProvider。

问题的一部分在于我很难找到关于如何使用内容提供程序来提供应用程序私有文件区域或如何使用SD卡上的自定义目录来提供文件的明确示例。Android文档对内容提供程序过程描述非常模糊,而复制/粘贴它们的XML代码无法构建。 - DavidG

3

您的图片位于应用程序沙盒中,因此您应该使用ContentProvider来为其他应用程序提供访问您的图像数据的外部访问权限。请记住,在Android中,预安装的应用程序没有比第三方应用程序更高的优先级-即使您想要使用默认应用程序,仍然需要授予权限。

否则,请查看相机应用程序中画廊活动的IntentFilter标记,以了解可以使用哪些Intent打开默认查看器的图像的参考。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接