Trying to get real path from uri for a selected image, but I got a null for filePath = cursor.getString()
while the cursor was not null at all.
Why?
In my app code, I have an onActivityResult returning from an mediastore image selection which I can get an URI for an image using the following:
Uri selectedImage = data.getData();
Then I try to get the real path of the selected image by calling the function as seen below.
realPath = RealPathUtil.getRealPathFromURI(this, selectedImage);
public static String getRealPathFromURI(Context context, Uri uri){
String filePath = "";
String[] column = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(uri,
column, null, null, null);
if( null != cursor ) {
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(column[0]);
if( columnIndex > -1 )
filePath = cursor.getString(columnIndex); //why got a null
}
cursor.close();
return filePath;
}
The step-debugging environment is an AVD with API 23. I ran the app and selected a pic from internal storage\Pictures folder.
Just after running the row of code,
filePath = cursor.getString(columnIndex);
I found filePath was null while then cursor.getCount() == 1 and columnIndex == 0.
Why is this?