Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image path from URI

Tags:

android

I am trying to get an Image file from the gallery:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
        GET_IMAGE_FROM_GALLERY);

The message "Selecture Picture" is not shown as a Toast.

And in onActivityResult();

Uri selectedImageUri = data.getData(); //log shows proper URI
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImageUri,
        projection, null, null, null);
int column_index = cursor
        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String selectedImagePath = cursor.getString(column_index);

cursor.getString(column_index) returns Null.

I am testing it on Nexus 4.

EDIT:

Looks like this is a problem with Android 4.4, I have see other apps failing too.

Convert content:// URI to actual path in Android 4.4

like image 312
Archie.bpgc Avatar asked Feb 14 '23 13:02

Archie.bpgc


1 Answers

Use this :

String selectedImagePath = null;
Uri selectedImageUri = data.getData();
Cursor cursor = activity.getContentResolver().query(
    selectedImageUri, null, null, null, null);
if (cursor == null) { 
    selectedImagePath = selectedImageUri.getPath();
} else {
    cursor.moveToFirst();
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    selectedImagePath = cursor.getString(idx);
}
like image 62
Venkata Krishna Avatar answered Feb 27 '23 17:02

Venkata Krishna