Android Select picture, get path by URI

Source: Internet
Author: User

    /** * Gets the absolute path of the picture according to the URI * * @param Context Context Object * @param URI of the image of the URL * @return if the URI corresponding to the picture exists, then return The absolute path of the picture, otherwise returns NULL */public static String Getrealpathfromuri (context context, Uri uri) {int sdkversion = B Uild. VERSION.        Sdk_int;        if (sdkversion >=) {//API >= return getRealPathFromUriAboveApi19 (context, URI);        } else {//API < return getRealPathFromUriBelowAPI19 (context, URI); }}/** * api19 the following (excluding api19), gets the absolute path of the picture according to the URI * * @param Context Context Object * @param URI of the picture  @return returns the absolute path of the picture if the corresponding picture of the URI exists, otherwise returns NULL */private static String getRealPathFromUriBelowAPI19 (context context,    Uri Uri) {return Getdatacolumn (context, URI, NULL, NULL); }/** * Suitable for api19 and above, get the absolute path of the picture according to the URI * * @param Context Context Object * @param URI of the picture of the URIs * @return if the URI to The expected picture exists, then returns the absolute path of the picture, otherwise returns null */@SuppressLint ("Newapi") private static STRing getRealPathFromUriAboveApi19 (context context, Uri Uri) {String filePath = null; if (Documentscontract.isdocumenturi (context, URI)) {//If it is a URI of document type, it is processed by the document ID Stri            ng DocumentID = Documentscontract.getdocumentid (URI); if (Ismediadocument (URI)) {//Mediaprovider//Use ': ' to split String id = documentid.split (":") [1]                ;                String selection = mediastore.images.media._id + "=?";                String[] Selectionargs = {ID};            FilePath = Getdatacolumn (Context, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection, Selectionargs); } else if (Isdownloadsdocument (URI)) {//Downloadsprovider uri Contenturi = Contenturis.withappendedid (uri                . Parse ("Content://downloads/public_downloads"), long.valueof (DocumentID));            FilePath = Getdatacolumn (context, Contenturi, NULL, NULL); }} else if ("content". Equalsignorecase (Uri.getschemE ())) {//If the URI of the content type FilePath = Getdatacolumn (context, URI, NULL, NULL); } else if ("File". Equals (Uri.getscheme ())) {//If it is a file-type URI, get the path to the image directly FilePath = Uri.getpath (        );    } return FilePath;  /** * Gets the _data column in the database table, which returns the file path of the URI * @return */private static String Getdatacolumn (context context,        Uri Uri, string selection, string[] selectionargs) {string path = null;        string[] projection = new String[]{mediastore.images.media.data};        cursor cursor = NULL;            try {cursor = Context.getcontentresolver (). Query (URI, projection, selection, Selectionargs, null); if (cursor! = NULL && Cursor.movetofirst ()) {int columnindex = Cursor.getcolumnindexorthrow (proje                Ction[0]);            Path = cursor.getstring (columnindex);  }} catch (Exception e) {if (cursor! = NULL) {cursor.close ();          }} return path; }/** * @param uri the URI to check * @return Whether the URI authority is mediaprovider */private stat    IC 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 downloadsprovider */privat E static Boolean isdownloadsdocument (Uri uri) {return ' com.android.providers.downloads.documents '. Equals (Uri.getau    Thority ()); }
    • Android 4.4 above and android4.4 previous version through the URI Get path method is not the same, so the code did the processing
    • android5.1.1 a picture of the uri→content://com.android.providers.media.documents/document/image:68

Android Select picture, get 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.