Android solves some phones cannot get to the album path by URI

Source: Internet
Author: User

 /** * 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

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.