Search code examples
androidcursorsd-cardmediastore

Why can't I see some of my pictures in the gallery?


I'm trying to read photos existing in the sdcard with MediaStore.Images.Media.DATA but I'm always getting an empty cursor !!! This is the code I'm using for the reading :

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

Cursor imagecursor = getContentResolver()
    .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, 
    MediaStore.Images.Media.DATA + " like ? ", 
    new String[] { "%"+eventName.trim()+"%" }, null);

imagecursor.setNotificationUri(getContentResolver(), 
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

this.imageUrls = new ArrayList<String>(); 
Log.i("imagecursor.getCount()", Integer.toString(imagecursor.getCount()));

for (int i = 0; i < imagecursor.getCount(); i++) {
    imagecursor.moveToPosition(i);
    int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
    imageUrls.add(imagecursor.getString(dataColumnIndex));
}

Of course I added this

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

to the AndroidManifest.xml


Solution

  • I found that the problem was with the Media Scanner so i solved it buy using a simple app called Scan Media witch scan the external storage and then the MediaStore will be able to find the new elements .