Android 4.4 KitKat above and below based on URI method of obtaining path

Source: Internet
Author: User

Reprint please indicate the source, thank you ~

Today, I was doing video editing, encountered this problem, before and after 1 hours to find and solve the problem, because I always think I remember the wrong, and later found that Huawei P6 with the Android4.4 system, and then I suddenly ...

First of all, what I am doing, I am making a video after filming, editing a video, this stuff is not difficult, the source, you are not afraid to believe?! In the Android source code Android.media.videoeditor have videoeditor This class, this is actually Google write the editor of the east, he called a few C + + write Lib library, these libraries Google also write well, we call under him can, Then I edit the video, to add a ringtone, according to the URI I wrote to get the path, always return null, very strange, finally found that Huawei P6 used 4.4 system ... The following is not wordy, directly on the code.


Here first look at the form of the URI before 4.4:

uri:content://media/extenral/images/media/17766

Is it familiar?

Look at the URI form 4.4 and later:

content://com.android.providers.media.documents/document/image%2706
I printed it out and I blew it. Looking at the P6 system, sure enough, 4.4 of the.

This is to say, and then turned over the API, found that 4.4 after the URI is not unique, not a unified format, so it's a common approach.


4.4 Get path by URI before:

Data is Uri,filename is a string that is used to save the path.

if (Data.getscheme (). toString (). CompareTo ("content") = = 0) {cursor = Getcontentresolver (). Query (Data,new string[] { Audio.Media.DATA}, NULL, NULL, NULL), if (Cursor.movetofirst ()) {filename = cursor.getstring (0);}} else if (Data.getscheme (). toString (). CompareTo ("file") = = 0)         //file:///the beginning of uri{filename = Data.tostring (); filename = data.tostring (). Replace ("file://", ""),//Replace file://if (!filename.startswith ("/mnt")) {//Plus '/mnt ' header filename + = "/mnt"; }}

4.4 Gets the path later based on the URI:

public class Getpathfromuri4kitkat {/** * is designed for Android4.4 to get the absolute path of the file from the URI, the previous method has not been good */@SuppressLint ("Newapi") public St Atic String GetPath (Final context context, final URI Uri) {Final Boolean iskitkat = Build.VERSION.SDK_INT >= Bu Ild. Version_codes.        KITKAT; Documentprovider if (Iskitkat && documentscontract.isdocumenturi (context, URI)) {//External Storageprovider if (Isexternalstoragedocument (URI)) {final String docId = Documentscontract.get                DocumentID (URI);                Final string[] split = Docid.split (":");                Final String type = split[0]; if ("PRIMARY". Equalsignorecase (Type)) {return environment.getexternalstoragedirectory () + "/" + split[                1]; }//TODO handle non-primary volumes}//Downloadsprovider else if (Isdow Nloadsdocument (URI)) {final String id = documentscontract.getdocumeNtid (URI); Final Uri Contenturi = Contenturis.withappendedid (Uri.parse ("Content://downloads/public_downloads"                ), long.valueof (ID));            Return Getdatacolumn (context, Contenturi, NULL, NULL); }//Mediaprovider else if (ismediadocument (URI)) {final String docId = Documentscon                Tract.getdocumentid (URI);                Final string[] split = Docid.split (":");                Final 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;                } final String selection = "_id=?"; Finalstring[] Selectionargs = new string[] {split[1]};            Return Getdatacolumn (context, Contenturi, selection, Selectionargs); }}//Mediastore (and general) else if ("content". Equalsignorecase (Uri.getscheme ())) {RE        Turn getdatacolumn (context, URI, NULL, NULL);        }//File else if ("file". Equalsignorecase (Uri.getscheme ())) {return Uri.getpath ();    } return null; }/** * Get The value of the data column for this Uri.     This was useful for * Mediastore Uris, and other file-based contentproviders.     * * @param context * the context.     * @param URI * The URI to query.     * @param selection * (Optional) Filter used in the query.     * @param Selectionargs * (Optional) Selection arguments used in the query.     * @return The value of the _data column, which is typically a file path. */public static String getdAtacolumn (context context, Uri Uri, String selection, string[] selectionargs) {cursor cursor = NULL;        Final String column = "_data";        Final string[] projection = {column};                    try {cursor = Context.getcontentresolver (). Query (URI, projection, selection, Selectionargs,            NULL); if (cursor! = NULL && Cursor.movetofirst ()) {final int column_index = Cursor.getcolumnindexorthrow                (column);            Return cursor.getstring (Column_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 '. 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 ()); }}
This part of the code draws on some examples on the web, pro-test available, welcome copy.

Android 4.4 KitKat above and below based on URI method of obtaining path

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.