首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >应用程序Android图像查看器

应用程序Android图像查看器
EN

Stack Overflow用户
提问于 2009-10-11 13:12:00
回答 3查看 10.6K关注 0票数 4

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

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

File.exists()返回true。

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

代码语言:javascript
运行
复制
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://,媒体://,图像:// )。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-10-11 20:25:07

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

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

代码语言:javascript
运行
复制
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中:

代码语言:javascript
运行
复制
<provider android:name=".FileContentProvider" android:authorities="uk.co.ashtonbrsc.examplefilecontentprovider" />

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

票数 6
EN

Stack Overflow用户

发布于 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“自动”处理的。

票数 3
EN

Stack Overflow用户

发布于 2009-10-11 13:41:04

您的图像位于您的应用程序沙箱中,因此您应该使用ContentProvider为其他应用程序提供对图像数据的外部访问。请记住,在Android中,预先安装的应用程序并不比第三方应用程序具有更高的优先级--即使您想使用默认的应用程序,仍然需要对您的数据给予许可。

否则,请查看照相机应用程序中的图片库活动的IntentFilter标记,以了解您可以使用什么Intent来使用默认查看器打开图像。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1550657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档