我正在开发适用于 Android 的 Cordova 应用程序,此应用程序必须从设备上传图像。测试是在装有 Android 4.0.4 的摩托罗拉 Xoom 平板电脑上进行的。
Cordova 版本是 2.7.0。
应用程序打开并正确调用了 ondeviceready。
用户按下一个按钮,就会触发以下函数:
capture: function(sourceType) {
navigator.camera.getPicture(this.onCaptureSuccess, this.onCaptureFail, {
quality: 20
,targetWidth: 100
,targetHeight: 100
,destinationType: Camera.DestinationType.FILE_URI
,sourceType: Camera.PictureSourceType.PHOTOLIBRARY
,correctOrientation: false
});
}
触发该功能后,将显示一个菜单,其中包含 4 个选项来选择文件的来源(ASTRO 文件管理器、ES 文件资源管理器、文件和库),并将以下消息添加到 Logcat:
05-11 13:30:51.400: D/DroidGap(11102): Paused the application!
05-11 13:30:51.400: D/CordovaWebView(11102): Handle the pause
我可以选择这四个中的任何一个,LogCat 会显示以下消息:
05-11 13:33:57.960: D/DroidGap(11409): onDestroy()
05-11 13:33:57.960: D/CordovaWebView(11409): >>> loadUrl(javascript:try{cordova.require('cordova/channel').onDestroy.fire();}catch(e){console.log('exception firing destroy event from native');};)
05-11 13:33:57.960: D/PluginManager(11409): init()
05-11 13:33:57.970: D/CordovaWebView(11409): >>> loadUrlNow()
05-11 13:33:58.120: D/OpenGLRenderer(11409): Flushing caches (mode 0)
05-11 13:33:58.350: D/OpenGLRenderer(11409): Flushing caches (mode 1)
一旦我选择了我要上传的图像,应用程序就会显示一条消息“不幸的是,这个过程......已经停止。” 并显示以下 LogCat 消息:
05-11 13:40:06.750: D/DroidGap(11886): DroidGap.onCreate()
05-11 13:40:06.770: D/CordovaWebView(11886): CordovaWebView is running on device made by: Motorola
05-11 13:40:06.770: D/JsMessageQueue(11886): Set native->JS mode to 2
05-11 13:40:06.770: D/DroidGap(11886): DroidGap.init()
05-11 13:40:06.770: D/CordovaWebView(11886): >>> loadUrl(file:///android_asset/www/index.html)
05-11 13:40:06.770: D/PluginManager(11886): init()
05-11 13:40:06.780: D/CordovaWebView(11886): >>> loadUrlNow()
05-11 13:40:06.780: D/DroidGap(11886): Incoming Result
05-11 13:40:06.780: D/DroidGap(11886): Request code = 18
05-11 13:40:06.780: D/DroidGap(11886): We have a callback to send this result to
05-11 13:40:06.780: D/AndroidRuntime(11886): Shutting down VM
05-11 13:40:06.780: W/dalvikvm(11886): threadid=1: thread exiting with uncaught exception (group=0x40a2f1f8)
05-11 13:40:06.800: E/AndroidRuntime(11886): FATAL EXCEPTION: main
05-11 13:40:06.800: E/AndroidRuntime(11886): java.lang.RuntimeException: Unable to resume activity {com.ubihealth/com.ubihealth.UbiHealth}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=18, result=-1, data=Intent { dat=content://media/external/images/media/7468 }} to activity {com.ubihealth/com.ubihealth.UbiHealth}: java.lang.NullPointerException
05-11 13:40:06.800: E/AndroidRuntime(11886): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2466)
05-11 13:40:06.800: E/AndroidRuntime(11886): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2494)
05-11 13:40:06.800: E/AndroidRuntime(11886): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2008)
05-11 13:40:06.800: E/AndroidRuntime(11886): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-11 13:40:06.800: E/AndroidRuntime(11886): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1169)
05-11 13:40:06.800: E/AndroidRuntime(11886): at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 13:40:06.800: E/AndroidRuntime(11886): at android.os.Looper.loop(Looper.java:137)
05-11 13:40:06.800: E/AndroidRuntime(11886): at android.app.ActivityThread.main(ActivityThread.java:4446)
05-11 13:40:06.800: E/AndroidRuntime(11886): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 13:40:06.800: E/AndroidRuntime(11886): at java.lang.reflect.Method.invoke(Method.java:511)
05-11 13:40:06.800: E/AndroidRuntime(11886): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-11 13:40:06.800: E/AndroidRuntime(11886): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-11 13:40:06.800: E/AndroidRuntime(11886): at dalvik.system.NativeStart.main(Native Method)
05-11 13:40:06.800: E/AndroidRuntime(11886): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=18, result=-1, data=Intent { dat=content://media/external/images/media/7468 }} to activity {com.ubihealth/com.ubihealth.UbiHealth}: java.lang.NullPointerException
05-11 13:40:06.800: E/AndroidRuntime(11886): at android.app.ActivityThread.deliverResults(ActivityThread.java:3002)
05-11 13:40:06.800: E/AndroidRuntime(11886): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2453)
05-11 13:40:06.800: E/AndroidRuntime(11886): ... 12 more
05-11 13:40:06.800: E/AndroidRuntime(11886): Caused by: java.lang.NullPointerException
05-11 13:40:06.800: E/AndroidRuntime(11886): at org.apache.cordova.DroidGap.onActivityResult(DroidGap.java:858)
05-11 13:40:06.800: E/AndroidRuntime(11886): at android.app.Activity.dispatchActivityResult(Activity.java:4649)
05-11 13:40:06.800: E/AndroidRuntime(11886): at android.app.ActivityThread.deliverResults(ActivityThread.java:2998)
05-11 13:40:06.800: E/AndroidRuntime(11886): ... 13 more
05-11 13:40:07.040: D/chromium(11886): Unknown chromium error: -6
05-11 13:40:07.070: D/CordovaNetworkManager(11886): Connection Type: none
关于我的错误可能是什么的任何想法?
更新:我已经用 Cordova 2.6.0 更新了我的项目并尝试了上面的代码,但它没有工作。现在我用 Cordova 2.7.0 创建了一个新项目,并从旧项目中复制了 Cordova 代码,它运行良好。