SweSnow SweSnow - 6 months ago 53
Java Question

Android KitKat Image selecting not returning anything

I've been trying to get the absolute image path of a image selected in the gallery on KitKat but it doesn't seem to be working out. My variable

IMAGE_FILEPATH
is always "" after whatever I do. Here's 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 going wrong? I've tried multiple solutions and nothing seems to work.

Answer

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

instead of :

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

So, here is what you can write under KK to make it works :

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

Comments