1

我的代码正在尝试使用查询获取用户设备上的视频列表,但我在三星 Galaxy S7、S8 等设备上遇到安全异常,这些设备是非常受欢迎的设备,不容忽视。

我的清单文件中确实有 READ_EXTERNAL_STORAGE 和 WRITE_EXTERNAL_STORAGE 权限。我的代码通过在应用启动时添加以下代码来使用新的权限模式:

private void requestPermission() {

        if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            Toast.makeText(this, "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
        } else {
            ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 15);
        }
    }

我的代码是:

Uri uri;
Cursor cursor;
uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

String[] projection = {MediaStore.Video.Media._ID,MediaStore.Video.Media.DURATION, MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.DATA};

final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
cursor = getApplicationContext().getContentResolver().query(uri, projection, null, null, orderBy + " DESC");

while (cursor.moveToNext()) {

    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);//Instead of "MediaStore.Images.Media.DATA" can be used "_data"
    Uri filePathUri = Uri.parse(cursor.getString(column_index));
    Uri var5 = Uri.withAppendedPath(Media.EXTERNAL_CONTENT_URI, ContentUtill.getLong(cursor));
    String fileName = filePathUri.getLastPathSegment().toString();

    VideoData var7 = new VideoData(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME)),
            var5,
            cursor.getString(cursor.getColumnIndexOrThrow( MediaStore.Video.Media.DATA)),
            cursor.getString(cursor.getColumnIndexOrThrow( MediaStore.Video.Media.DURATION)));
    this.videoList.add(var7);
}

我在上面的代码中的以下行看到了安全异常:

cursor = getApplicationContext().getContentResolver().query(uri, projection, null, null, orderBy + " DESC");

异常的堆栈跟踪是:

Caused by: java.lang.SecurityException: 
  at android.os.Parcel.readException (Parcel.java:1599)
  at android.database.DatabaseUtils.readExceptionFromParcel (DatabaseUtils.java:188)
  at android.database.DatabaseUtils.readExceptionFromParcel (DatabaseUtils.java:140)
  at android.content.ContentProviderProxy.query (ContentProviderNative.java:421)
  at android.content.ContentResolver.query (ContentResolver.java:498)
  at android.content.ContentResolver.query (ContentResolver.java:441)
  at video.format.converter.view.SelectVideoActivity.getVideoList (SelectVideoActivity.java:92)
  at video.format.converter.view.SelectVideoActivity.access$000 (SelectVideoActivity.java:37)
  at video.format.converter.view.SelectVideoActivity$loadVideo.doInBackground (SelectVideoActivity.java:210)
  at video.format.converter.view.SelectVideoActivity$loadVideo.doInBackground (SelectVideoActivity.java:194)
  at android.os.AsyncTask$2.call (AsyncTask.java:295)
  at java.util.concurrent.FutureTask.run (FutureTask.java:237)

我的代码有什么问题,以及获取适用于所有类型的新旧设备的视频列表的推荐方法是什么。(API 14+)

4

0 回答 0