Laire Laire - 2 months ago 20
Android Question

MediaStore Image width and height null

when I crawl through the MediaStore, there are images where the entry for height and weight is null.

The files exist and I can show them with the image browser.

Cursor mCursor = _context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,null,null,null,MediaStore.Images.Media.DEFAULT_SORT_ORDER);
if(mCursor != null){
mCursor.moveToFirst();
while(!mCursor.isAfterLast()) {
Log.d("DEBUG","PIC PATH: " + mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DATA)));
Log.d("DEBUG","WIDTH: " + mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.WIDTH)));
Log.d("DEBUG","HEGHT: " + mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.HEIGHT)));
mCursor.moveToNext();
}
mCursor.close();
}



09-12 18:17:30.861 13002-13002/com.google.observer D/DEBUG: PIC PATH:
/storage/emulated/0/Pictures/Instagram/IMG_20160813_221335.jpg

09-12 18:17:30.861 13002-13002/com.google.observer D/DEBUG: WIDTH:
null

09-12 18:17:30.861 13002-13002/com.google.observer D/DEBUG: HEGHT:
null


The File has the dimensions: 3366*2988 and I can open it in the file browser.

EDIT

I tried it with columns (see answer from Pavneet Singh), but same error. The most pictures works, only a few have this problem:

final String[] columns = {MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA, MediaStore.Images.Media.WIDTH, MediaStore.Images.Media.HEIGHT, MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.SIZE};
Cursor mCursor = _context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,columns,null,null,MediaStore.Images.Media.DEFAULT_SORT_ORDER);
if(mCursor != null){
mCursor.moveToFirst();
while(!mCursor.isAfterLast()) {
Log.d("DEBUG","PIC PATH: " + mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DATA)));
Log.d("DEBUG","WIDTH: " + mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.WIDTH)));
Log.d("DEBUG","HEGHT: " + mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.HEIGHT)));
mCursor.moveToNext();
}
mCursor.close();
}

Answer

MediaStore don't return all the columns values automatically due to performance reasons as mentioned in docs so you need to explicitly pass the projection to MediaStore query.

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


Cursor mCursor = _context.getContentResolver().
                     query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                          columns,null,null,
                          MediaStore.Images.Media.DEFAULT_SORT_ORDER);

Some find it confusing but it enhance the performance because there can be too much details related with an image like Date, Time, Title, Place, Latitude, Longitude etc which will degrade the performance if the query is for all images stored(usually it is plus can be thousands of images ) so hence less data, less time hence better performance.

Details : Some images might not have the required information maybe they are from another app or downloaded from some site etc so use the update info.

Update : you can follow this approach for your few problematic images