Android19 above and below URI-to-path methods

Source: Internet
Author: User

Android 19 +

/*** Get file path from URI designed for Android4.4 above*/     Public StaticString GetPath (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)) {Logutils.loginfostar ("Isexternalstoragedocument"); 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)) {Logutils.loginfostar ("Isdownloadsdocument"); 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)) {Logutils.loginfostar ("Ismediadocument"); 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 was useful for * Mediastore Uris, and other file-based contentproviders. *     * @paramcontext the context. * @paramuri the 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 Staticstring Getdatacolumn (context context, Uri Uri, String selection, string[] Selec Tionargs) {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; }    /**     * @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 ()); }

Android 19 below:

  /*** Try to return the absolute file path from the given Uri * *@paramContext *@paramURI *@returnThe file path or null*/     Public StaticString Uri2path (FinalContext Context,FinalURI Uri) {        if(NULL= = URI)return NULL; FinalString 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[]{mediastore.images.imagecolumns.data},NULL,NULL,NULL); if(NULL!=cursor) {                if(Cursor.movetofirst ()) {intindex =Cursor.getcolumnindex (MediaStore.Images.ImageColumns.DATA); if(Index = =-1) {Index=Cursor.getcolumnindexorthrow (MediaStore.Images.Media.DATA); }                    if(Index >-1) {Data=cursor.getstring (index);            }} cursor.close (); }        }        returndata; }

Android19 above and below URI-to-path methods

Related Article

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.