Cordova 应用程序在尝试访问相机时崩溃

Posted

技术标签:

【中文标题】Cordova 应用程序在尝试访问相机时崩溃【英文标题】:Cordova application crashes when trying access the Camera 【发布时间】:2018-01-14 15:43:47 【问题描述】:

我有一个 Cordova 应用程序(在 android 上运行),我实现了 Cordova 相机插件,我有一个简单的“点击”事件,它调用以下代码,然后弹出消息:

“允许 MyHQAPP 访问您设备上的照片、媒体和文件吗?”

我点击“允许”,应用程序立即关闭??我检查了 ADB 错误日志,可以在下面看到以下错误

任何人都可以提出问题所在 - 请注意我正在使用 ngCordova 插件来实现相机功能 http://ngcordova.com/docs/plugins/camera/

08-07 11:33:19.712 14954 15032 E PluginManager: Uncaught exception from plugin
08-07 11:33:19.712 14954 15032 E PluginManager: android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/com.mytechnologies.myapp_2017/cache/.Pic.jpg exposed beyond app through ClipData.Item.getUri()
08-07 11:33:19.712 14954 15032 E PluginManager:         at android.os.StrictMode.onFileUriExposed(StrictMode.java:1799)
08-07 11:33:19.712 14954 15032 E PluginManager:         at android.net.Uri.checkFileUriExposed(Uri.java:2346)
08-07 11:33:19.712 14954 15032 E PluginManager:         at android.content.ClipData.prepareToLeaveProcess(ClipData.java:845)
08-07 11:33:19.712 14954 15032 E PluginManager:         at android.content.Intent.prepareToLeaveProcess(Intent.java:8941)
08-07 11:33:19.712 14954 15032 E PluginManager:         at android.content.Intent.prepareToLeaveProcess(Intent.java:8926)
08-07 11:33:19.712 14954 15032 E PluginManager:         at android.app.Instrumentation.execStartActivity(Instrumentation.java:1690)
08-07 11:33:19.712 14954 15032 E PluginManager:         at android.app.Activity.startActivityForResult(Activity.java:4291)
08-07 11:33:19.712 14954 15032 E PluginManager:         at org.apache.cordova.CordovaActivity.startActivityForResult(CordovaActivity.java:342)
08-07 11:33:19.712 14954 15032 E PluginManager:         at android.app.Activity.startActivityForResult(Activity.java:4249)
08-07 11:33:19.712 14954 15032 E PluginManager:         at org.apache.cordova.CordovaInterfaceImpl.startActivityForResult(CordovaInterfaceImpl.java:66)
08-07 11:33:19.712 14954 15032 E PluginManager:         at org.apache.cordova.camera.CameraLauncher.takePicture(CameraLauncher.java:259)
08-07 11:33:19.712 14954 15032 E PluginManager:         at org.apache.cordova.camera.CameraLauncher.callTakePicture(CameraLauncher.java:233)
08-07 11:33:19.712 14954 15032 E PluginManager:         at org.apache.cordova.camera.CameraLauncher.execute(CameraLauncher.java:168)
08-07 11:33:19.712 14954 15032 E PluginManager:         at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:98) 
08-07 11:33:19.712 14954 15032 E PluginManager:         at org.apache.cordova.PluginManager.exec(PluginManager.java:132)
08-07 11:33:19.712 14954 15032 E PluginManager:         at org.apache.cordova.CordovaBridge.jsExec(CordovaBridge.java:57)
08-07 11:33:19.712 14954 15032 E PluginManager:         at org.apache.cordova.engine.SystemExposedJsApi.exec(SystemExposedJsApi.java:41)
08-07 11:33:19.712 14954 15032 E PluginManager:         at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
08-07 11:33:19.712 14954 15032 E PluginManager:         at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:7)
08-07 11:33:19.712 14954 15032 E PluginManager:         at android.os.Handler.dispatchMessage(Handler.java:102)
08-07 11:33:19.712 14954 15032 E PluginManager:         at android.os.Looper.loop(Looper.java:154)
08-07 11:33:19.712 14954 15032 E PluginManager:         at android.os.HandlerThread.run(HandlerThread.java:61)
08-07 11:33:21.836 13427 13427 E SimcardDataModel: provide unsupported slot id 0

在我的 js 控制器中

$scope.uploadFromCamera = function () 
    console.log('Upload from Camera');

    var options = 
        quality: 75,
        destinationType: Camera.DestinationType.DATA_URL,
        sourceType: Camera.PictureSourceType.CAMERA,
        allowEdit: true,
        encodingType: Camera.EncodingType.JPEG,
        targetWidth: 300,
        targetHeight: 300,
        popoverOptions: CameraPopoverOptions,
        saveToPhotoAlbum: false
    ;

    $cordovaCamera.getPicture(options).then(function (imageData) 
        $scope.imgURI = "data:image/jpeg;base64," + imageData;
        console.log($scope.imgURI);
        console.log($scope);
    , function (err) 
        // An error occured. Show a message to the user
    );

//科尔多瓦信息 节点版本:v6.10.3 科尔多瓦版本:6.0.0

Config.xml 文件:

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.mytechnologies.fakename_2017" versionCode='20002' version="2.0.2" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
  <name>MyAPP</name>
  <description>
    A sample Apache Cordova application that responds to the deviceready event.
  </description>
  <author email="dev@cordova.apache.org" href="http://cordova.io">
    Apache Cordova Team
  </author>
  <content src="index.html" />
  <plugin name="cordova-plugin-whitelist" spec="1" />
  <access origin="*" />
  <allow-intent href="http://*/*" />
  <allow-intent href="https://*/*" />
  <allow-intent href="tel:*" />
  <allow-intent href="sms:*" />
  <allow-intent href="mailto:*" />
  <allow-intent href="geo:*" />
  <platform name="android">
    <allow-intent href="market:*" />
  </platform>
  <platform name="ios">
    <allow-intent href="itms:*" />
    <allow-intent href="itms-apps:*" />
  </platform>
  <engine name="android" spec="~5.2.2" />
  <icon src="res/icon.png" />
</widget>

插件:

cordova-plugin-camera,cordova-plugin-compat,cordova-plugin-whitelist

cordova-plugin-camera 2.1.1“相机” cordova-plugin-whitelist 1.3.0“白名单”

【问题讨论】:

插件版本也很有用... @n00dl3 好主意刚刚添加了这些 这个插件版本好像不兼容 API 24+ (Android 7.0)....***.com/questions/38200282/… 尝试升级到最新的cordova-plugin-camera 2.4.1 我刚刚检查了代码,它使用了FileProvider API 干杯 @n00dl3 将更新我的 sdk(这将永远需要大声笑)并尝试使用 2.4.1 插件安装,因为我在运行命令安装相机插件时注意到它总是安装 2.1.1 【参考方案1】:

尝试将cordova-plugin-camera的版本改为2.4.1

【讨论】:

为我工作将 cordova-plugin-camera 更新到 2.4.1 并将平台更新到 android 6.1.0

以上是关于Cordova 应用程序在尝试访问相机时崩溃的主要内容,如果未能解决你的问题,请参考以下文章

Android Cordova 5.0+ 相机崩溃

Cordova - 由于联系权限,Android 应用程序在启动时崩溃

检索照片时相机崩溃

使用相机插件拍摄新照片时,PhoneGap 应用程序崩溃

Cordova 相机插件返回绿色图片 Windows Phone 8.1

尝试退出插件 webview 时,带有集成插件的 Cordova 应用程序在 webview 上崩溃(android 测试)