public classUriUtils {
/**
* Get a file path from a Uri. This will get the the path for Storage Access
* Framework Documents, as well as the _data field for the MediaStore and
* other file-based ContentProviders.
*
*@paramcontextThe context.
*@paramuriThe Uri to query.
*/
public staticStringgetPath(finalContext context, finalUri uri) {
final booleanisKitKat = Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if(isKitKat && DocumentsContract.isDocumentUri(context,uri)) {
// ExternalStorageProvider
if(isExternalStorageDocument(uri)) {
finalString docId = DocumentsContract.getDocumentId(uri);
finalString[] split = docId.split(":");
finalString type = split[0];
if("primary".equalsIgnoreCase(type)) {
returnEnvironment.getExternalStorageDirectory() +"/"+ split[1];
}
//TODO handle non-primary volumes
}
// DownloadsProvider
else if(isDownloadsDocument(uri)) {
finalString id = DocumentsContract.getDocumentId(uri);
finalUri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"),Long.valueOf(id));
returngetDataColumn(context,contentUri, null, null);
}
// MediaProvider
else if(isMediaDocument(uri)) {
finalString docId = DocumentsContract.getDocumentId(uri);
finalString[] split = docId.split(":");
finalString type = split[0];
Uri contentUri =null;
if("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}else if("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
}else if("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
finalString selection ="_id=?";
finalString[] selectionArgs =newString[] {
split[1]
};
returngetDataColumn(context,contentUri,selection,selectionArgs);
}
}
// MediaStore (and general)
else if("content".equalsIgnoreCase(uri.getScheme())) {
returngetDataColumn(context,uri, null, null);
}
// File
else if("file".equalsIgnoreCase(uri.getScheme())) {
returnuri.getPath();
}
return null;
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
*@paramcontextThe context.
*@paramuriThe Uri to query.
*@paramselection(Optional) Filter used in the query.
*@paramselectionArgs(Optional) Selection arguments used in the query.
*@returnThe value of the _data column, which is typically a file path.
*/
public staticStringgetDataColumn(Context context,Uri uri,String selection,
String[] selectionArgs) {
Cursor cursor =null;
finalString column ="_data";
finalString[] projection = {
column
};
try{
cursor = context.getContentResolver().query(uri,projection,selection,selectionArgs,
null);
if(cursor !=null&& cursor.moveToFirst()) {
final intcolumn_index = cursor.getColumnIndexOrThrow(column);
returncursor.getString(column_index);
}
}finally{
if(cursor !=null)
cursor.close();
}
return null;
}
/**
*@paramuriThe Uri to check.
*@returnWhether the Uri authority is ExternalStorageProvider.
*/
public static booleanisExternalStorageDocument(Uri uri) {
return"com.android.externalstorage.documents".equals(uri.getAuthority());
}
/**
*@paramuriThe Uri to check.
*@returnWhether the Uri authority is DownloadsProvider.
*/
public static booleanisDownloadsDocument(Uri uri) {
return"com.android.providers.downloads.documents".equals(uri.getAuthority());
}
/**
*@paramuriThe Uri to check.
*@returnWhether the Uri authority is MediaProvider.
*/
public static booleanisMediaDocument(Uri uri) {
return"com.android.providers.media.documents".equals(uri.getAuthority());
}
}