FileNotFoundException用于从sdcard获取图像

时间:2014-03-11 06:01:42

标签: java android gridview

我试图获取sdcard中的所有图像并填入gridview ,,,,

这是我的代码,用于获取图像frd 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;
    }

以上代码为某些图像返回FileNotFoundException,但在gridview中填充可用图像,gridview为enter image description here

前五个网格没有图像,而选择这些图像会在其他活动中显示空白区域

抛出的异常是

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)

1 个答案:

答案 0 :(得分:1)

我认为媒体商店的路径错误 尝试检查路径中的真实文件。

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);
像这样。