ContentProvider有时会返回不存在的图像

时间:2015-07-06 13:59:54

标签: android android-contentprovider android-gallery android-loader

我为多图像选择器创建了一个活动。图像是通过Loader请求的,如下所示:

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String[] projection = {
        MediaStore.Files.FileColumns._ID,
        MediaStore.Files.FileColumns.DATA
    };

    switch (id) {
        case IMAGES_LOADER:
            String selection;
            String[] selectionArgs;
            if (Build.VERSION.SDK_INT > 16) {
                selection = MediaStore.Files.FileColumns.SIZE + ">100 and " +
                    MediaStore.Files.FileColumns.WIDTH + ">? and " +
                    MediaStore.Files.FileColumns.HEIGHT + ">?";
                selectionArgs = new String[] { String.valueOf(mMinWidth), String.valueOf(mMinHeight) };
            } else {
                selection = MediaStore.Files.FileColumns.SIZE + ">0";
                selectionArgs = new String[0];
            }

            return new CursorLoader(this, MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                projection, selection, selectionArgs,
                MediaStore.Images.Media._ID + " desc"
            );
        default:
            return null;
    }
}

之后我通过UIL将图像加载到ImageViews,如下所示:

String data = cursor.getString(mDataColumnIndex);
try {
    mImageLoader.displayImage(Uri.fromFile(new File(data)).toString(), holder.image);
} catch (Exception e) {
    …
}

但有时在catch区块中出现FileNotFoundException。我记录了这样的文件名,在文件管理器中检查过,它们确实不存在。

这是关于一个具有此效果的文件的日志。文件管理器不显示它,但它的大小和尺寸已从ContentProvider成功加载:

… Displaying image [/storage/emulated/0/DCIM/Camera/IMG_20150703_195503.jpg], size 2448 x 3264, file length 1418535 (ImagePickerActivity.java:241)
… Showing uri: [file:///storage/emulated/0/DCIM/Camera/IMG_20150703_195503.jpg] (ImagePickerActivity.java:253)
… /storage/emulated/0/DCIM/Camera/IMG_20150703_195503.jpg: open failed: ENOENT (No such file or directory)
    java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/IMG_20150703_195503.jpg: open failed: ENOENT (No such file or directory)
            at libcore.io.IoBridge.open(IoBridge.java:456)
            at java.io.FileInputStream.<init>(FileInputStream.java:76)
            at java.io.FileInputStream.<init>(FileInputStream.java:103)
            at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStreamFromFile(BaseImageDownloader.java:142)
            at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStream(BaseImageDownloader.java:78)
            at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.getImageStream(BaseImageDecoder.java:83)
            at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.decode(BaseImageDecoder.java:69)
            at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeImage(LoadAndDisplayImageTask.java:307)
            at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:262)
            at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:128)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
            at libcore.io.Posix.open(Native Method)
            at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
            at libcore.io.IoBridge.open(IoBridge.java:442)
            at java.io.FileInputStream.<init>(FileInputStream.java:76)
            at java.io.FileInputStream.<init>(FileInputStream.java:103)
            at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStreamFromFile(BaseImageDownloader.java:142)
            at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStream(BaseImageDownloader.java:78)
            at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.getImageStream(BaseImageDecoder.java:83)
            at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.decode(BaseImageDecoder.java:69)
            at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeImage(LoadAndDisplayImageTask.java:307)
            at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:262)
            at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:128)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)

为什么退回这些物品?怎么避免这个?

0 个答案:

没有答案