About Android4.4 's picture path gets, if the format of the URI comes back in two ways

Source: Internet
Author: User

About Android4.4 's picture path gets, if the format of the URI comes back in two ways
1, content://com.android.providers.media.documents/document/image:3951
2, content://media/external/images/media/3951

Workaround:
1),
When >=4.4
if (Documentscontract.isdocumenturi (context, Contenturi)) {
String Wholeid = Documentscontract.getdocumentid (Contenturi);
String id = wholeid.split (:) [1];
string[] column = {MediaStore.Images.Media.DATA};
String sel = mediastore.images.media._id + =?;
cursor cursor = context.getcontentresolver (). Query (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column,
SEL, new string[] {ID}, NULL);
int columnindex = Cursor.getcolumnindex (column[0]);
if (Cursor.movetofirst ()) {
FilePath = cursor.getstring (columnindex);
}
Cursor.close ();

When <4.4
}else{
string[] projection = {MediaStore.Images.Media.DATA};
cursor cursor = context.getcontentresolver (). Query (Contenturi, projection, NULL, NULL, NULL);
int column_index = Cursor.getcolumnindexorthrow (MediaStore.Images.Media.DATA);
Cursor.movetofirst ();
FilePath = cursor.getstring (Column_index);
}
2),
public static String GetPath (final context context, final URI Uri) {

Final Boolean Iskitkat = Build.VERSION.SDK_INT >= build.version_codes. KITKAT;

Documentprovider
if (Iskitkat && documentscontract.isdocumenturi (context, URI)) {
Externalstorageprovider
if (Isexternalstoragedocument (URI)) {
Final String docId = Documentscontract.getdocumentid (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 (Isdownloadsdocument (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 = Documentscontract.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=?";
Final string[] Selectionargs = new string[] {
SPLIT[1]
};

Return Getdatacolumn (context, Contenturi, selection, Selectionargs);
}
}
Mediastore (and General)
else if ("content". Equalsignorecase (Uri.getscheme ())) {

Return to the remote address
if (Isgooglephotosuri (URI))
return Uri.getlastpathsegment ();

Return 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. Useful for
* Mediastore Uris, and other file-based contentproviders.
*
* @param 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 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". Equals (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". Equals (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 (Uri.getauthority ());
}
}

About Android4.4 's picture path gets, if the format of the URI comes back in two ways

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.