Question

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.

Was it helpful?

Solution

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

OTHER TIPS

moveToFirst returns a boolean indicating if moving to that position resulted in data.

boolean haveData;
haveData = cursor.moveToFirst();

if (haveData) {
 cursor.getString(.....

That's what you should be doing, which is just good, defensive programming. Now, why your cursor is empty, that's going to take more digging to your contentResolver.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top