Ext.: http://blog.csdn.net/q445697127/article/details/40537945
/*** Get image absolute path according to URI, resolve Android4.4 above version URI conversion *@paramactivity *@paramImageuri *@authoryaoxing * @date 2014-10-12*/@TargetApi (19) Public StaticString Getimageabsolutepath (Activity context, Uri Imageuri) {if(Context = =NULL|| Imageuri = =NULL) return NULL; if(Android.os.Build.VERSION.SDK_INT >= Android.os.Build.VERSION_CODES. KITKAT &&Documentscontract.isdocumenturi (context, Imageuri)) { if(Isexternalstoragedocument (Imageuri)) {String docId=Documentscontract.getdocumentid (Imageuri); String[] Split= Docid.split (":"); String type= Split[0]; if("PRIMARY". Equalsignorecase (Type)) { returnEnvironment.getexternalstoragedirectory () + "/" + split[1]; } } Else if(Isdownloadsdocument (Imageuri)) {String ID=Documentscontract.getdocumentid (Imageuri); Uri Contenturi= Contenturis.withappendedid (Uri.parse ("Content://downloads/public_downloads"), long.valueof (ID)); returnGetdatacolumn (context, 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=NewString[] {split[1] }; returngetdatacolumn (context, Contenturi, selection, Selectionargs); } } //Mediastore ( and General) Else if("Content". Equalsignorecase (Imageuri.getscheme ())) { //Return to the remote address if(Isgooglephotosuri (Imageuri))returnimageuri.getlastpathsegment (); returnGetdatacolumn (context, Imageuri,NULL,NULL); } //File Else if("File". Equalsignorecase (Imageuri.getscheme ())) { returnImageuri.getpath (); } return NULL; } Public Staticstring Getdatacolumn (context context, Uri Uri, String selection, string[] selectionargs) {cursor 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 ()) { intindex =cursor.getcolumnindexorthrow (column); returncursor.getstring (index); } } finally { if(Cursor! =NULL) Cursor.close (); } return NULL; } /** * @paramuri the URI to check. * @returnWhether The Uri authority is externalstorageprovider. */ Public Static Booleanisexternalstoragedocument (Uri uri) {return"Com.android.externalstorage.documents". Equals (Uri.getauthority ()); } /** * @paramuri the URI to check. * @returnWhether The Uri authority is downloadsprovider. */ Public Static Booleanisdownloadsdocument (Uri uri) {return"Com.android.providers.downloads.documents". Equals (Uri.getauthority ()); } /** * @paramuri the URI to check. * @returnWhether The Uri authority is mediaprovider. */ Public Static Booleanismediadocument (Uri uri) {return"Com.android.providers.media.documents". Equals (Uri.getauthority ()); } /** * @paramuri the URI to check. * @returnWhether The Uri authority is Google Photos. */ Public Static BooleanIsgooglephotosuri (Uri uri) {return"Com.google.android.apps.photos.content". Equals (Uri.getauthority ()); }
Get picture absolute path according to URI, resolve Android4.4 above version URI conversion