Uri converted to path (absolute path) adaptation to all versions

Source: Internet
Author: User
Tags split versions
public class Uritopathutil {public static String Getrealfilepath (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, new string[] {MediaStore.Images.ImageColumns.DATA}, NULL, NULL, NULL); if (null! = cursor) {if (Cursor.movetofirst ()) {int index = Cursor.getcolumnindex (Me
                    DiaStore.Images.ImageColumns.DATA);
                    if (Index >-1) {data = cursor.getstring (index);
            }} cursor.close (); } if (data = = null) {DATA = Getimageabsolutepath (context, URI);
    }} return data;
    public static Uri GetURI (final String filePath) {return uri.fromfile (new File (FilePath)); /** * Get picture absolute path according to URI, resolve Android4.4 above version URI conversion * * @param context * @param Imageuri * @author y aoxing * @date 2014-10-12 */@TargetApi public static String Getimageabsolutepath (context context, U
        Ri 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)) {return environmEnt.getexternalstoragedirectory () + "/" + split[1]; }} else if (Isdownloadsdocument (Imageuri)) {String id = documentscontract.getdocumentid (imag
                Euri);
                Uri Contenturi = Contenturis.withappendedid (Uri.parse ("Content://downloads/public_downloads"), Long.valueOf (ID));
            Return Getdatacolumn (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 = new string[] {split[1]};
            Return Getdatacolumn (context, Contenturi, selection, Selectionargs); }}//Mediastore (and general) else if ("content". Equalsignorecase (Imageuri.getscheme ())) {/ /Return the remote address if (Isgooglephotosuri (Imageuri)) return imageuri.getlastpathsegmen
            T ();
        Return Getdatacolumn (context, 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) {Cu
        Rsor 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 ();
    } return null;
     }/** * @param uri * The URI to check.
     * @return Whether The Uri authority is externalstorageprovider. */public static Boolean isexternalstoragedocument (Uri uri) {return ' com.android.externalstorage.documents '. E
    Quals (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 '. equ ALS (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.
    Getauthority ());
     }/** * @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 (U
    Ri.getauthority ());
 }
}

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.