代码之家  ›  专栏  ›  技术社区  ›  DavidG

应用程序中的Android图像查看器

  •  4
  • DavidG  · 技术社区  · 16 年前

    我正在尝试启动一个图像,它用内置的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):正在启动 活动:目的{ 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 }

    不管我将URI模式更改为什么(例如,content://、file://、media://、image://)。

    3 回复  |  直到 15 年前
        1
  •  6
  •   Intrications    16 年前

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

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

    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 在你的行动中,观察意图。

        2
  •  3
  •   CommonsWare    16 年前

    选项1:创建 ContentProvider 要在应用程序的专用文件区域外提供文件,请使用 ACTION_VIEW Intent 在那 content:// Uri .

    选项2:将文件移动到SD卡,并使用 Acthon视图 意图 尿嘧啶尿路感染 以及适当的mime类型。Android不会自动将文件扩展名与mime类型关联,因此您需要告诉 意图 什么类型的mime类型 尿嘧啶尿路感染 指向。这是为您“自动”处理的 内容提供者 .

        3
  •  3
  •   Dimitar Dimitrov    16 年前

    您的图像在应用程序沙盒中,因此应该使用 ContentProvider 为其他应用程序提供对图像数据的外部访问。请记住,在Android中,预安装的应用程序没有比第三方应用程序更高的优先级-即使您想使用默认应用程序,您仍然必须授予数据权限。

    否则,请查看画廊活动的 IntentFilter 相机应用程序中的标签,作为参考什么 Intent 可以使用默认查看器打开图像。