The path address of the Android picture file and the URI are converted to each other

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.