为什么$ cordovaImagePicker插件在Marshmallow上崩溃了?

时间:2017-02-01 11:01:15

标签: android cordova ionic-framework

我是整合$ cordovaImagePicker,它在kit kat和棒棒糖上工作正常,但最近我安装了它在Marshmallow上崩溃的相同应用程序。 以下是我正在使用的代码:

$cordovaImagePicker.getPictures(options).then(function (results) {
    for (var i = 0; i < results.length; i++) {
        $scope.imagePath = results[i];
    }
}, function(error) {
    console.log('Error: ' + JSON.stringify(error));
});
  

java.lang.SecurityException:Permission Denial:read   com.android.providers.media.MediaProvider uri   content:// media / external / images / media from pid = 10669,uid = 10319   需要android.permission.READ_EXTERNAL_STORAGE,或   grantUriPermission()                                                       在android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:605)                                                       在android.content.ContentProvider $ Transport.enforceReadPermission(ContentProvider.java:480)                                                       在android.content.ContentProvider $ Transport.query(ContentProvider.java:211)                                                       在android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)                                                       在android.os.Binder.execTransact(Binder.java:453)

任何帮助?

1 个答案:

答案 0 :(得分:1)

花了一些时间之后,我发现Android Marshmallow有不同的许可机制,它在运行时请求许可,所以需要照顾它。以下是代码

var permissions = cordova.plugins.permissions;
permissions.hasPermission(permissions.READ_EXTERNAL_STORAGE, 
            function(status) {
               if (status.hasPermission) {
                   $cordovaImagePicker.getPictures(options).then(function (results) {
                        for (var i = 0; i < results.length; i++) {
                            $scope.imagePath = results[i];
                        }
                    }, function(error) {
                          console.log('Error: ' + JSON.stringify(error));    // In case of error
                    });
                } else {
                  //permission is not granted case, now preparing for granting permission
                    permissions.requestPermission(
                        permissions.READ_EXTERNAL_STORAGE,
                        function(status) {
                          if(!status.hasPermission) {
                            console.log();
                          }
                        },
                        function(status) {
                            console.log();
                        }
                    );                      
                }
           }, function(status) {
              console.log("permission not available");
            });

首先,如果未授予权限,我会检查是否授予权限,然后请求许可。有关下面的详细信息,请参阅链接

https://www.npmjs.com/package/cordova-plugin-android-permissions