简体   繁体   English

FileNotFoundException用于从SD卡提取图像

[英]FileNotFoundException for fetching image from sdcard

Im trying to fetch all images in sdcard and populate in gridview,,,, 我试图获取sdcard中的所有图像并在gridview中填充,

this is my code,, for fetching image fron sdcard 这是我的代码,用于获取图像。

private ArrayList<CustomGallery> getGalleryPhotos() {
        ArrayList<CustomGallery> galleryList = new ArrayList<CustomGallery>();

        try {
            final String[] columns = { MediaStore.Images.Media.DATA,
                    MediaStore.Images.Media._ID };
            final String orderBy = MediaStore.Images.Media._ID;

            @SuppressWarnings("deprecation")
            Cursor imagecursor = managedQuery(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
                    null, null, orderBy);
            if (imagecursor != null && imagecursor.getCount() > 0) {

                while (imagecursor.moveToNext()) {
                    CustomGallery item = new CustomGallery();

                    int dataColumnIndex = imagecursor
                            .getColumnIndex(MediaStore.Images.Media.DATA);

                    item.sdcardPath = imagecursor.getString(dataColumnIndex);
                    galleryList.add(item);
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

        // show newest photo at beginning of the list
        Collections.reverse(galleryList);
        return galleryList;
    }

the above code returns FileNotFoundException for some images but populates available images in gridview the gridview is 上面的代码为某些图像返回FileNotFoundException,但在gridview中填充可用图像 在此处输入图片说明

the top five grid has no images ,,while selecting those images gives blank area in other activity 前五个网格中没有图像,选择这些图像会在其他活动中显示空白区域

the exception thrown is 抛出的异常是

03-11 11:00:40.540: E/ImageLoader(4145): /mnt/storage/Kushalam/_profileImage/CroppedImage.jpg (No such file or directory)
03-11 11:00:40.540: E/ImageLoader(4145): java.io.FileNotFoundException: /mnt/storage/Kushalam/_profileImage/CroppedImage.jpg (No such file or directory)
03-11 11:00:40.540: E/ImageLoader(4145):    at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
03-11 11:00:40.540: E/ImageLoader(4145):    at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
03-11 11:00:40.540: E/ImageLoader(4145):    at java.io.FileInputStream.<init>(FileInputStream.java:80)
03-11 11:00:40.540: E/ImageLoader(4145):    at org.apache.harmony.luni.internal.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:82)
03-11 11:00:40.540: E/ImageLoader(4145):    at org.apache.harmony.luni.internal.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:180)
03-11 11:00:40.540: E/ImageLoader(4145):    at java.net.URL.openStream(URL.java:645)
03-11 11:00:40.540: E/ImageLoader(4145):    at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStreamFromFile(BaseImageDownloader.java:121)
03-11 11:00:40.540: E/ImageLoader(4145):    at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStream(BaseImageDownloader.java:82)
03-11 11:00:40.540: E/ImageLoader(4145):    at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.saveImageOnDisc(LoadAndDisplayImageTask.java:340)
03-11 11:00:40.540: E/ImageLoader(4145):    at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:232)

I think mediastore return wrong path. 我认为Mediastore返回错误的路径。
Try check real file in path. 尝试检查路径中的真实文件。

CustomGallery item = new CustomGallery();

                int dataColumnIndex = imagecursor
                        .getColumnIndex(MediaStore.Images.Media.DATA);

                item.sdcardPath = imagecursor.getString(dataColumnIndex);
                File temp = new File( item.sdcardPath );
                if( temp.exists() )
                     galleryList.add(item);

like this. 像这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM