[Phonegap+sencha Touch] Mobile development 29 Android Navigator.camera.getPicture get the true path of the picture

Source: Internet
Author: User

PhoneGap Photo Plugin Select the image in the library, the code is as follows:

Navigator.camera.getPicture (function (URI) {console.log (URI);//Get the URI of the picture here}, function (err) {Console.log (err);}, {    quality:70,    DestinationType:navigator.camera.DestinationType.FILE_URI,    sourcetype: Navigator.camera.PictureSourceType.PHOTOLIBRARY,    savetophotoalbum:true});


The path to the picture returned is this:

Before Android 4.4: CONTENT://MEDIA/EXTERNAL/IMAGES/MEDIA/62

Android 4.4:content://com.android.providers.media.documents/document/image:62


This path is called the content URI, not the real path of the picture, the real path should be this:file:///storage/emulated/0/DCIM/Camera/1406773746740.jpg.


Workaround:

Open plug-in file Org.apache.cordova.camera\src\android\cameralauncher.java

Add the following code to the inside:

/** * Get A file path from a Uri. This would get the path for Storage Access * Framework Documents, as well as the _data field for the Mediastore and * o Ther file-based contentproviders. * * @param the context. * @param uri the URI to query.  * @author Paulburke */@SuppressLint ("Newapi") 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)) {//EXTERNALSTORAGEP             Rovider 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.getdocumen            TId (URI); Final Uri Contenturi = Contenturis.withappendedid (Uri.parse ("Content://downloads/public_downloads"), L            Ong.valueof (ID));        Return Getdatacolumn (context, Contenturi, NULL, NULL); }//Mediaprovider else if (ismediadocument (URI)) {final String docId = Documentscontract.getdoc            Umentid (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; } finalString 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 the Remo        Te 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. This was 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 (col            UMN);        Return cursor.getstring (index);    }} finally {if (cursor! = NULL) cursor.close (); } return null; /** * @param URI of 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 of 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 of 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 of 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 ());}

Then find the following code:

if (this.targetheight = =-1 && this.targetwidth = =-1 &&        (desttype = = File_uri | | desttype = = Native_u RI) &&!this.correctorientation) {    this.callbackContext.success (uri.tostring ());}

Change it into this

if (this.targetheight = =-1 && this.targetwidth = =-1 &&        (desttype = = File_uri | | desttype = = Native_u RI) &&!this.correctorientation) {    Context context = This.webView.getContext (). Getapplicationcontext ();    This.callbackContext.success (GetPath (context, URI));}


Remember to import the package at the top:

Import Android.annotation.suppresslint;import Android.content.contenturis;import Android.content.context;import Android.os.build;import android.provider.DocumentsContract;





Welcome to Sencha Touch + Phonegap Group: 194182999

Joint Learning Communication (blogger qq:479858761)

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.