2015-10-14 148 views
9

我使用MediaScannerConnection调用其scanFile方法,以将图像添加到设备库。由于权限拒绝,Android 6上的MediaScannerConnection失败

E/DatabaseUtils:但在Android的6当我执行它,我收到此异常java.lang.SecurityException异常:权限拒绝: 阅读com.android.providers.media.MediaProvider URI 内容://媒体从PID = 22984 /外部/ fs_id,UID = 10078要求 android.permission.READ_EXTERNAL_STORAGE,或grantUriPermission()

E/iu.UploadsManager:java.lang.SecurityExceptio N:许可拒绝: 读数com.android.providers.media.MediaProvider URI 内容:从PID = 22984 //媒体/外部/ fs_id,UID = 10078要求 android.permission.READ_EXTERNAL_STORAGE,或grantUriPermission()

任何帮助?

+0

我面临着同样的问题。我试图将视频上传到Youtube,但我得到了同样的错误。你找到解决方案吗? – TOP

+0

不是。我注意到,当我用WhatsApp拍照时,在聊天中,我可以在LogCat中看到同样的错误。所以也许这是一个Android 6的问题,因为新的运行时权限.... –

+0

我试图添加标志Intent.FLAG_GRANT_READ_URI_PERMISSION,但它似乎无法正常工作。 – TOP

回答

0

这是处理权限的新方法的结果。您的应用程序现在需要获得运行时权限并准备好被拒绝。

在你的onCreate(或地方),你检查,并可能要求许可:

  if (ContextCompat.checkSelfPermission(this, 
      Manifest.permission.READ_EXTERNAL_STORAGE) 
      != PackageManager.PERMISSION_GRANTED) { 

     // Should we show an explanation? 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
       Manifest.permission.READ_EXTERNAL_STORAGE)) { 

      // Show an expanation to the user *asynchronously* -- don't block 
      // this thread waiting for the user's response! After the user 
      // sees the explanation, try again to request the permission. 

     } else { 

      // No explanation needed, we can request the permission. 

      ActivityCompat.requestPermissions(this, 
        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 
        MY_PERMISSIONS_REQUEST_READ_MEDIA); 

      // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 
      // app-defined int constant. The callback method gets the 
      // result of the request. 
     } 
    } 

然后准备好接受这样的权限:

@Override 
public void onRequestPermissionsResult(int requestCode, 
             String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST_READ_MEDIA: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
      } else { 
       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 
      } 
      return; 
     } 
    } 
} 
+0

Nop。正如你所看到的,这个异常不是由我的应用程序引发的,而是DatabaseUtils和UploadsManager。而且,运行时权限不会影响我的应用程序(还),因为我没有针对API 23. –

+0

答案仍然与列出的问题相关,并且适用于其他人遇到类似问题。我已经提出了删除downvote。有没有办法设置一次覆盖应用中所有事件的权限?而不是必须弄清楚应用程序可能需要什么地方来请求参赛者? – Theo