2015-10-14 150 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