我正在尝试用内置的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));
它不断地涌出:
dat=file:///data/data/com.davidgoemans.myapp/files/current.png 10-11 13:09:24.367: INFO/ActivityManager(564):启动活动:意向{ act=android.intent.action.VIEW } 10-11 13:09:24.367:错误/myapp(2166):未发现异常occuredandroid.content.ActivityNotFoundException:处理意图{ act=android.intent.action.VIEW dat=file:///data/data/com.davidgoemans.myapp/files/current.png }
无论我将uri模式更改为(例如,内容://,file://,媒体://,图像:// )。
发布于 2009-10-11 20:25:07
一种方法是实现上下文提供程序,使其他应用程序能够访问您的数据。
创建一个包含以下内容的新类:
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" />
然后,您应该能够在您的"content://uk.co.ashtonbrsc.examplefilecontentprovider/" + the full path to the image
意图中使用ACTION_VIEW。
发布于 2009-10-11 13:23:58
选项1:创建一个ContentProvider
以在应用程序的私有文件区域内提供文件,然后在该content://
Uri
上使用一个ACTION_VIEW
Intent
。
选项2:将文件移动到SD卡,并在ACTION_VIEW
上使用Intent
,并使用适当的MIME类型。Android不会自动将文件扩展名与MIME类型相关联,因此您需要告诉Intent
Uri
指向哪种MIME类型。这是用ContentProvider
“自动”处理的。
发布于 2009-10-11 13:41:04
您的图像位于您的应用程序沙箱中,因此您应该使用ContentProvider
为其他应用程序提供对图像数据的外部访问。请记住,在Android中,预先安装的应用程序并不比第三方应用程序具有更高的优先级--即使您想使用默认的应用程序,仍然需要对您的数据给予许可。
否则,请查看照相机应用程序中的图片库活动的IntentFilter
标记,以了解您可以使用什么Intent
来使用默认查看器打开图像。
https://stackoverflow.com/questions/1550657
复制相似问题