来自App的Android Image Viewer

我正在尝试使用内置的Android图像查看器启动一个写入我的应用程序目录的图像。 此图像已在应用程序的不同部分写入app目录。 获取以下文件时:

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):exceptionoccuredandroid.content.ActivityNotFoundException:找不到处理Intent的活动{act = android.intent.action.VIEW dat = file:/// data /data/com.davidgoemans.myapp/files/current.png}

无论我将uri架构更改为什么(例如,content://,file://,media://,image://)。

一种方法是实现上下文提供程序,以便其他应用程序访问您的数据。

创建一个新类,其中包含:

 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:

  

然后,您应该能够使用"content://uk.co.ashtonbrsc.examplefilecontentprovider/" + the full path to the image ACTION_VIEW意图中"content://uk.co.ashtonbrsc.examplefilecontentprovider/" + the full path to the image

选项#1:创建一个ContentProvider以从应用程序的私有文件区域提供文件,然后对该content://使用ACTION_VIEW Intent content:// Uri

选项#2:将文件移动到SD卡,并在Uri上使用ACTION_VIEW Intent ,并使用相应的MIME类型。 Android不会自动将文件扩展名与MIME类型相关联,因此您需要告诉Intent Uri指向的MIME类型。 这是使用ContentProvider “自动”处理的。

您的图像位于应用程序沙箱中,因此您应使用ContentProvider为其他应用程序提供对图像数据的外部访问权限。 请注意,在Android中,预安装的应用没有比第三方应用更高的优先级 – 即使您想使用默认应用,您仍然必须授予您的数据权限。

否则,请查看Camera应用程序中Gallery活动的IntentFilter标记,以获取可用于使用默认查看器打开图像的Intent的参考。