【问题标题】:Android Image Viewer from App来自 App 的 Android 图像查看器
【发布时间】:2009-10-11 13:12:00
【问题描述】:

我正在尝试使用内置的 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));

而且它一直在生产:

10-11 13:09:24.367: 信息/活动管理器(564):正在启动 活动:意图{ 行动=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: 未找到处理 Intent { 的活动 行动=android.intent.action.VIEW dat=file:///data/data/com.davidgoemans.myapp/files/current.png }

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

【问题讨论】:

    标签: java android


    【解决方案1】:

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

    创建一个新类,包含:

    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" />
    

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

    【讨论】:

    • 谢谢,今晚试试。我几乎已经实现了,执行应用程序似乎太过分了。出色的设计/架构,但对于基本用途而言过于复杂。
    • 注意:覆盖 getType() 需要我设置 mime 类型,例如:@Override public String getType(Uri uri) { return "image/png"; }
    • 这在我的应用程序中运行良好,多亏了你们两个,......我很惊讶很少有应用程序可以消耗我的意图。 Intent.ACTION_VIEW 是错误的选择吗?我应该选择 image/jpg 以外的 mime 类型吗?
    【解决方案2】:

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

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

    【讨论】:

    • 部分问题是我正在努力寻找如何使用内容提供程序为我的应用程序的私有文件区域提供服务的可靠示例,或者如何使用 SD 卡上的自定义目录来服务我的文件。 Android 文档非常模糊地描述了 Content Provider 过程,并且复制/粘贴它们的 XML 不会构建。
    【解决方案3】:

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

    否则,请查看“相机”应用程序中 Gallery 活动的 IntentFilter 标签,作为参考,您可以使用 Intent 来使用默认查看器打开图像。

    【讨论】:

      猜你喜欢
      • 2023-02-03
      • 1970-01-01
      • 2018-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多