java 来自应用程序的 Android 图像查看器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1550657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:03:07  来源:igfitidea点击:

Android Image Viewer from App

javaandroid

提问by DavidG

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:

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

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

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

File.exists() returns true.

File.exists() 返回真。

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

如何启动内置​​的 Android 图像查看器来查看此文件?目前我正在做:

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

And it keeps churning out:

它不断涌现:

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 }

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:错误/myapp(2166):发生异常android.content.ActivityNotFoundException:没有找到处理意图的活动{act=android.intent.action.VIEW dat=file:///data /data/com.davidgoemans.myapp/files/current.png }

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

不管我如何将 uri 架构更改为(例如,content://、file://、media://、image://)。

回答by Intrications

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

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

Create a new class containing:

创建一个包含以下内容的新类:

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

}

Add the content provider to your AndroidManifest.xml:

将内容提供程序添加到您的 AndroidManifest.xml:

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

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

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

回答by CommonsWare

Option #1: Create a ContentProviderto serve up the file out of your app's private file area, then use an ACTION_VIEWIntenton that content://Uri.

选项#1:创建一个ContentProvider以从您的应用程序的私有文件区域中提供文件,然后ACTION_VIEWIntent在该content://Uri.

Option #2: Move the file to the SD card, and use an ACTION_VIEWIntenton the Uriand also with the appropriate MIME type. Android does not automatically associate file extensions with MIME types, so you need to tell the Intentwhat sort of MIME type the Uripoints to. This is handled for you "automatically" with the ContentProvider.

方案2:将文件移动到SD卡,并使用ACTION_VIEWIntentUri也有相应的MIME类型。Android 不会自动将文件扩展名与 MIME 类型关联,因此您需要告知指向Intent的 MIME 类型Uri。这是使用ContentProvider.

回答by Dimitar Dimitrov

Your image is within your application sandbox, so you should use ContentProviderto give other apps external access to your image data. Keep in mind that in Android, pre-installed apps don't have higher priority than third-party apps - you still have to give permission to your data, even if you want to use the default apps.

您的图像位于您的应用程序沙箱中,因此您应该使用它ContentProvider来让其他应用程序从外部访问您的图像数据。请记住,在 Android 中,预装应用程序的优先级并不高于第三方应用程序 - 即使您想使用默认应用程序,您仍然必须授予数据权限。

Otherwise, take a look at the Gallery activity's IntentFiltertags in the Camera application, for a reference what Intentyou can use to open an image with the default viewer.

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