从应用程序的Andr​​oid图像浏览器应用程序、图像、浏览器、oid

2023-09-06 13:22:25 作者:江佐

我试图推出其写入我的应用程序目录中内置的Andr​​oid的图像浏览器的图像。这个图像已经写在该应用该应用目录的不同部分。当得到以下文件:

I'm trying to launch an image which is written to my application directory with the builtin Android image viewer. This image has been written in a different part of the app to the app directory. When getting the following file:

super.getFilesDir()+/current.png

File.exists()返回true。

File.exists() returns true.

我如何可以启动内置的Andr​​oid的图像查看器来查看此文件? 目前我在做什么:

How can i launch the builtin Android image viewer to view this file? Currently i'm doing:

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:   信息/ ActivityManager(564):启动   活动:意向{   ACT = android.intent.action.VIEW   DAT =文件:///data/data/com.davidgoemans.myapp/files/current.png   10月11日} 13:09:24.367:   错误/ MyApp的(2166):异常   occuredandroid.content.ActivityNotFoundException:   无活动来处理意向{   ACT = android.intent.action.VIEW   DAT =文件:///data/data/com.davidgoemans.myapp/files/current.png   }

10-11 13:09:24.367: INFO/ActivityManager(564): Starting activity: 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): Exception occuredandroid.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///data/data/com.davidgoemans.myapp/files/current.png }

无论什么我更改URI架构。(如:内容://,文件://,媒体://,图像://)

irrespective of what i change the uri schema to ( eg, content://, file://, media://, image:// ).

推荐答案

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

One way is to implement a context provider to give other applications access to your data.

创建包含一个新的类:

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");
   }

}

添加内容提供商到您的Andr​​oidManifest.xml:

Add the content provider to your AndroidManifest.xml:

<provider android:name=".FileContentProvider" android:authorities="uk.co.ashtonbrsc.examplefilecontentprovider" />

您应该然后就可以使用内容://uk.co.ashtonbrsc.examplefilecontentprovider/+的完整路径图像在ACTION_VIEW意图

You should then be able to use "content://uk.co.ashtonbrsc.examplefilecontentprovider/" + the full path to the image in your ACTION_VIEW intent.