Java – Android KitKat image selection returns nothing

Android KitKat image selection returns nothing… here is a solution to the problem.

Android KitKat image selection returns nothing

I’ve been trying to get the absolute image path of the image selected in the KitKat gallery, but it doesn’t seem to be successful. No matter what I do, my variable IMAGE_FILEPATH is always “”. Here is the code for my onActivityResult().

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

if (resultCode != Activity.RESULT_OK) return;
    if (null == data) return;
    Uri originalUri = null;
    if (requestCode == 1) {
        JB!!!
            Uri uri = data.getData();

if (uri != null) {

try {
                     User had pick an image.
                    String[] filePathColumn = { MediaStore.Images.Media.DATA };

Cursor cursor = getContentResolver()
                            .query(uri,
                                    filePathColumn, null, null, null);

cursor.moveToFirst();

IMAGE_FILEPATH = cursor.getString(0);
                    cursor.close();
                } catch (Exception e) {
                    Crouton.makeText(this, "Failed to get image", Style.ALERT).show();
                }
            }
    } else if (requestCode == 2) {
        KK!!!
            Uri uri = data.getData();

if (uri != null) {
                try {
                    if( uri == null ) {
                        IMAGE_FILEPATH = uri.getPath();
                    } else {
                        String[] projection = { MediaStore.Images.Media.DATA };
                        Cursor cursor = managedQuery(uri, projection, null, null, null);
                        if( cursor != null ){
                            int column_index = cursor
                                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                            cursor.moveToFirst();
                            IMAGE_FILEPATH =  cursor.getString(column_index);
                        } else {
                            IMAGE_FILEPATH = uri.getPath();
                        }
                    }

} catch (Exception e) {
                    Crouton.makeText(this, "Failed to get image", Style.ALERT).show();
                }
            }
    }
    Resource.toast(IMAGE_FILEPATH);

super.onActivityResult(requestCode, resultCode, data);
}

What’s wrong? I’ve tried multiple solutions but nothing seems to work.

Solution

In KitKat, the Gallery returns a URI like this: content://com.android.providers.media.documents/document/image:1

Instead of:

content://media/external/images/media/1

So, you can write the following under KK to make it work:

if (uri != null) {
    try {
        if( uri == null ) {
            IMAGE_FILEPATH = uri.getPath();
        } else {
             get the id of the image selected by the user
            String wholeID = DocumentsContract.getDocumentId(data.getData());
            String id = wholeID.split(":")[1];

String[] projection = { MediaStore.Images.Media.DATA };
            String whereClause = MediaStore.Images.Media._ID + "=?";
            Cursor cursor = getContentResolver().query(getUri(), projection, whereClause, new String[]{id}, null);
            if( cursor != null ){
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                if (cursor.moveToFirst()) {
                    IMAGE_FILEPATH = cursor.getString(column_index);
                }

cursor.close();
            } else {
                IMAGE_FILEPATH = uri.getPath();
            }
        }
    } catch (Exception e) {
        Crouton.makeText(this, "Failed to get image", Style.ALERT).show();
    }
}

And the function I used:

private Uri getUri() {
    String state = Environment.getExternalStorageState();
    if(!state.equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
        return MediaStore.Images.Media.INTERNAL_CONTENT_URI;
    }

return MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}

These posts helped me: retrieve absolute path when select image from gallery kitkat android and Get real path from URI, Android KitKat new storage access framework

Related Problems and Solutions