【问题标题】:Image in getExternalFilesDir()getExternalFilesDir() 中的图像
【发布时间】:2012-05-22 14:41:09
【问题描述】:

我有一些图像存储在getExternalFilesDir() 中,我正在尝试在 android 库 (cooliris) 中显示这些图像。 现在我一直在这样做:

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(imgPath,"image/*");
startActivity(intent);

但是什么也没发生。 我已将 setDataAndType 更改为:

intent.setDataAndType(Uri.fromFile(new File(imgPath)),"image/*");

这样可以正常工作,但画廊从黑屏到显示我的图像需要 5-10 秒。

无论如何要解决这个问题或任何更好的方法?

【问题讨论】:

    标签: android android-gallery cooliris


    【解决方案1】:

    通过实现文件内容提供程序,您将能够避免这 5-10 秒的延迟

    import java.io.File;
    import java.io.FileNotFoundException;
    
    import android.content.ContentProvider;
    import android.content.ContentValues;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.ParcelFileDescriptor;
    
    public class FileContentProvider extends ContentProvider {
        private static final String AUTHORITY = "content://com.yourprojectinfo.fileprovider";
    
        public static Uri constructUri(String url) {
            Uri uri = Uri.parse(url);
            return uri.isAbsolute() ? uri : Uri.parse(AUTHORITY + url);
        }
    
        public static Uri constructUri(File file) {
            Uri uri = Uri.parse(file.getAbsolutePath());
            return uri.isAbsolute() ? uri : Uri.parse(AUTHORITY
                    + file.getAbsolutePath());
        }
    
        @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) {
            return "image/jpeg";
        }
    
        @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");
        }
    
    }
    

    然后就可以调用了

    Uri uri = FileContentProvider.constructUri(file);
    Intent intent = new Intent();
    intent.setAction(android.content.Intent.ACTION_VIEW);
    intent.setDataAndType(uri,"image/*");
    startActivity(intent);
    

    这是一个奇怪的解决方法,但我认为它与 android 如何使用 URI 打开图像有关。他们的 openFile(Uri uri, String mode) 方法是错误的/损坏的/无法正确解析 URI。虽然我不是 100% 确定,但我发现这种解决方法是有效的。

    别忘了在清单中注册到提供者

    【讨论】:

    • 它不起作用...我收到以下错误:java.lang.UnsupportedOperationException: Not supported by this provider 05-22 18:53:26.959: E/DatabaseUtils(26789): at com.mypackage.provider.FileContentProvider.query( FileContentProvider.java:58)
    • 嗯,有些东西正在查询那个错误的 URI。任何东西都不应该查询该 URI。它有没有说什么在调用它?
    • 在错误的乞求处说:05-22 19:04:57.259: E/DatabaseUtils(26789): Writing exception to parcel“它说什么在调用查询吗?”我怎么看出来的?
    • 它说“UnsupportedOperationException”的地方应该有一堆显示调用堆栈的行(导致错误的调用方法)
    • at android.content.ContentProvider$Transport.bulkQuery(ContentProvider.java:174) at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:111) at android.os.Binder.execTransact(Binder.java:320) at dalvik.system.NativeStart.run(Native Method)
    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-27
    • 1970-01-01
    • 2012-04-24
    • 2014-05-27
    相关资源
    最近更新 更多