The URI address of an Android file is generally as follows:
content://media/external/images/media/62026
This is the URI of a picture, so how do we get its path in the file system based on this URI?
Actually very simple, directly on the code:
/** Try to return the absolute file path from the given URI * * @param context * @param uri * @return the file path or Null*/ Public StaticString Getrealfilepath (Final context context, final URI Uri) {if(NULL= = URI)return NULL; Final String Scheme=Uri.getscheme (); String Data=NULL; if(Scheme = =NULL) Data=Uri.getpath (); Else if(ContentResolver.SCHEME_FILE.equals (SCHEME)) {Data=Uri.getpath (); } Else if(ContentResolver.SCHEME_CONTENT.equals (SCHEME)) {cursor cursor= Context.getcontentresolver (). Query (URI,NewString[] {Imagecolumns.data},NULL,NULL,NULL ); if(NULL!=cursor) { if(Cursor.movetofirst ()) {intindex =Cursor.getcolumnindex (Imagecolumns.data); if(Index >-1) {Data=cursor.getstring (index); }} cursor.close (); } } returndata;}
So what if we have a path address for a picture and how do we get its URI?
String type =Utils.ensurenotnull (Intent.gettype ()); LOG.D (TAG,"URI is"+URI); if(Uri.getscheme (). Equals ("file") && (Type.contains ("image/")) {String path=Uri.getencodedpath (); LOG.D (TAG,"path1 is"+path); if(Path! =NULL) {Path=Uri.decode (path); LOG.D (TAG,"path2 is"+path); Contentresolver CR= This. Getcontentresolver (); StringBuffer Buff=NewStringBuffer (); Buff.append ("("). Append (Images.ImageColumns.DATA). Append ("="). Append ("'"+ Path +"'"). Append (")"); Cursor cur=cr.query (Images.Media.EXTERNAL_CONTENT_URI,Newstring[] {images.imagecolumns._id}, buff.tostring (),NULL,NULL); intindex =0; for(Cur.movetofirst ();!cur.isafterlast (); cur. MoveToNext ()) {Index=Cur.getcolumnindex (images.imagecolumns._id); //set _id valueindex =Cur.getint (index); } if(Index = =0) { // do nothing}Else{Uri uri_temp=Uri. Parse ("content://media/external/images/media/"+index); LOG.D (TAG,"Uri_temp is"+uri_temp); if(Uri_temp! =NULL) {URI=uri_temp; } } } }
The path address of the Android picture file and the URI are converted to each other