/** * Get file path by Uri * * @param imageuri * @return Path * * Private String GetPath (Uri Imageuri) { if (Imageuri = = null) return null; if (Android.os.Build.VERSION.SDK_INT >= Android.os.Build.VERSION_CODES. KITKAT && Documentscontract.isdocumenturi (this, Imageuri)) {if (Isexternalstoragedocument (Imageuri)) { String docId = Documentscontract.getdocumentid (Imageuri); string[] split = Docid.split (":"); String type = split[0]; if ("PRIMARY". Equalsignorecase (Type)) {return environment.getexternalstoragedirectory () + "/" + split[ 1]; }} else if (Isdownloadsdocument (Imageuri)) {String id = documentscontract.getdocumentid (imageu RI); Uri Contenturi = Contenturis.withappendedid (Uri.parse ("Content://downloads/public_downloads"), Long.valueOf (ID)); Return Getdatacolumn (This, contenTUri, NULL, NULL); } else if (Ismediadocument (Imageuri)) {String docId = Documentscontract.getdocumentid (Imageuri); string[] split = Docid.split (":"); String 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; } String selection = mediastore.images.media._id + "=?"; string[] Selectionargs = new string[] {split[1]}; Return Getdatacolumn (this, Contenturi, selection, Selectionargs); }}//Mediastore (and general) else if ("content". Equalsignorecase (Imageuri.getscheme ())) { Return the remote address if (Isgooglephotosuri (Imageuri)) return IMAGEURI.GETLASTPA Thsegment (); Return Getdatacolumn (this, imageuri, NULL, NULL); }//File else if ("file". Equalsignorecase (Imageuri.getscheme ())) {return Imageuri.getpath (); } return null; public static String Getdatacolumn (context context, Uri Uri, String selection, string[] selectionargs) {curso R cursor = NULL; String column = MediaStore.Images.Media.DATA; string[] projection = {column}; try {cursor = Context.getcontentresolver (). Query (URI, projection, selection, Selectionargs, null); if (cursor! = NULL && Cursor.movetofirst ()) {int index = cursor.getcolumnindexorthrow (column); Return cursor.getstring (index); }} finally {if (cursor! = NULL) cursor.close (); } returnNull }/** * @param uri the URI to check. * @return Whether The Uri authority is externalstorageprovider. */public static Boolean isexternalstoragedocument (Uri uri) {return ' com.android.externalstorage.documents '. equ ALS (Uri.getauthority ()); }/** * @param uri the URI to check. * @return Whether The Uri authority is downloadsprovider. */public static Boolean isdownloadsdocument (Uri uri) {return ' com.android.providers.downloads.documents '. Equal S (uri.getauthority ()); }/** * @param uri the URI to check. * @return Whether The Uri authority is mediaprovider. */public static Boolean ismediadocument (Uri uri) {return ' com.android.providers.media.documents '. Equals (uri.ge Tauthority ()); }/** * @param uri the URI to check. * @return Whether The Uri authority is Google Photos. */public static Boolean Isgooglephotosuri (Uri uri) {return ' com.google.android.apps.photos.content '. equALS (Uri.getauthority ()); }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android solves some phones cannot get to the album path by URI