0

我在使用 Android 7.0 的多台设备的 Crashlytics 上遇到异常情况-

Caused by java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/video/media from pid=7070, uid=12033 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
       at android.os.Parcel.readException(Parcel.java:1684)
       at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
       at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
       at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
       at android.content.ContentResolver.query(ContentResolver.java:532)
       at android.content.CursorLoader.loadInBackground(CursorLoader.java:64)
       at android.content.CursorLoader.loadInBackground(CursorLoader.java:56)
       at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312)
       at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
       at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:66)
       at android.os.AsyncTask$2.call(AsyncTask.java:305)
       at java.util.concurrent.FutureTask.run(FutureTask.java:237)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
       at java.lang.Thread.run(Thread.java:761)

我研究并发现当我们在 Android 6.0+ 设备中不采取运行时异常时会发生此异常。但是在我的项目中,我在任何需要的地方都使用了运行时权限。崩溃没有指定我项目的任何特定行。下面的代码我用来从用户那里获取运行时权限 -

  uploadVideo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Build.VERSION.SDK_INT >= 23)
                        getPermission();
                    else
                        uploadVideo();

                }
            });


private void getPermission() {
        String[] params = null;
        String writeExternalStorage = Manifest.permission.WRITE_EXTERNAL_STORAGE;
        String readExternalStorage = Manifest.permission.READ_EXTERNAL_STORAGE;

        int hasWriteExternalStoragePermission = ActivityCompat.checkSelfPermission(this, writeExternalStorage);
        int hasReadExternalStoragePermission = ActivityCompat.checkSelfPermission(this, readExternalStorage);
        List<String> permissions = new ArrayList<String>();

        if (hasWriteExternalStoragePermission != PackageManager.PERMISSION_GRANTED)
            permissions.add(writeExternalStorage);
        if (hasReadExternalStoragePermission != PackageManager.PERMISSION_GRANTED)
            permissions.add(readExternalStorage);

        if (!permissions.isEmpty()) {
            params = permissions.toArray(new String[permissions.size()]);
        }
        if (params != null && params.length > 0) {
            ActivityCompat.requestPermissions(MainActivity.this,
                    params,
                    100);
        } else
            uploadVideo();
    }

 @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 100: {

                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    uploadVideo();
                }
            }
  }
}

更新

根据阿里的回答,我需要分别检查这两个权限是否被授予。 但是按照这个,如果授予了权限组中的任何权限,同一组中的另一个权限也会被自动授予。并且读写外部存储权限属于同一组。

4

1 回答 1

0

如果您错过<uses-permission android:name="com.android.providers.media.MediaProvider"/>了 AndroidManifest.xml,请添加并重试。

于 2018-11-03T14:45:35.707 回答