I am using PhoneGap + Sencha Touch on an application. I have managed to take a picture and upload to a server. I am having a problem selecting a picture from the photolibrary.
selectPicture: function () {
navigator.camera.getPicture(this.uploadPicture.bind(this), this.getPictureError.bind(this), {
// camera options
quality: 50,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
});
}
I think there is something wrong with the destinationType property. In my uploadPicture function the imageURI is returning a directory like: ‘content://media/external/images/media/[some random number]’.
I thought the imageURI should be something like: ‘file://storage/sdcard/DCIM/Camera/myimage.jpg’
I tried using window.resolveLocalFileSystemURI and it works fine if you are taking a picture, but not when selecting a picture. Here is my code when I want to select a picture form the photolibrary (I am using sencha touch 2):
selectPicture: function () {
navigator.camera.getPicture(this.resolveFileURI.bind(this), this.getPictureError.bind(this), {
// camera options
quality: 50,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
});
},
resolveFileURI: function (entry) {
console.log(entry);
window.resolveLocalFileSystemURI(entry, this.uploadPicture.bind(this), this.fail.bind(this));
},
uploadPicture: function (entry) {
console.log(entry.fullPath)
}
According to logcat I don’t even make it into the resolveFileURI function. In fact my application crashes. The error in logcat is:
10-23 14:39:24.552: E/AndroidRuntime(2975): FATAL EXCEPTION: main
10-23 14:39:24.552: E/AndroidRuntime(2975): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=17, result=-1, data=Intent { dat=content://media/external/images/media/584 }} to activity {com.dwmobile.android/com.dwmobile.android.AndroidSencha}: java.lang.NullPointerException
10-23 14:39:24.552: E/AndroidRuntime(2975): at android.app.ActivityThread.deliverResults(ActivityThread.java:3141)
10-23 14:39:24.552: E/AndroidRuntime(2975): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
10-23 14:39:24.552: E/AndroidRuntime(2975): at android.app.ActivityThread.access$1100(ActivityThread.java:130)
10-23 14:39:24.552: E/AndroidRuntime(2975): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
10-23 14:39:24.552: E/AndroidRuntime(2975): at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 14:39:24.552: E/AndroidRuntime(2975): at android.os.Looper.loop(Looper.java:137)
10-23 14:39:24.552: E/AndroidRuntime(2975): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-23 14:39:24.552: E/AndroidRuntime(2975): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 14:39:24.552: E/AndroidRuntime(2975): at java.lang.reflect.Method.invoke(Method.java:511)
10-23 14:39:24.552: E/AndroidRuntime(2975): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-23 14:39:24.552: E/AndroidRuntime(2975): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-23 14:39:24.552: E/AndroidRuntime(2975): at dalvik.system.NativeStart.main(Native Method)
10-23 14:39:24.552: E/AndroidRuntime(2975): Caused by: java.lang.NullPointerException
10-23 14:39:24.552: E/AndroidRuntime(2975): at org.apache.cordova.CameraLauncher.onActivityResult(CameraLauncher.java:453)
10-23 14:39:24.552: E/AndroidRuntime(2975): at org.apache.cordova.DroidGap.onActivityResult(DroidGap.java:823)
10-23 14:39:24.552: E/AndroidRuntime(2975): at android.app.Activity.dispatchActivityResult(Activity.java:5192)
10-23 14:39:24.552: E/AndroidRuntime(2975): at android.app.ActivityThread.deliverResults(ActivityThread.java:3137)
That is correct on Android. You are able to use the content URI’s to set the image source attribute. You can even pass that uri into FileTransfer.upload. However, if you want to get a real file type uri then send it into window.resolveLocalFileSystemURI and the success callback will give you the file path.