Reputation: 117
Im trying to fetch all images in sdcard and populate in 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
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)
Upvotes: 0
Views: 151
Reputation: 703
I think mediastore return wrong path.
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.
Upvotes: 1