This is a code which returns a file path given the URI.
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
//String[] projection = { MediaColumns.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
This works for images in Gallery because the projection is MediaStore.Images. How do get file path for all other directories? The file can be a document type and it can be in any location of sd card. What projection should I search for the file path?
The problem is here. It checks for projections in Images
only. So if file is of different type, you will not get a path. Exception
will be returned. Also its better to use:
int column_index = cursor.getColumnIndex(MediaColumns.DATA);
and use your onw try-catch
block. This will prevent your app from crashing
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.