2011-06-10 17 views
1

我嘗試了以下方法,但總是返回PackageManager.PERMISSION_DENIED,雖然我知道我有權讀取該文件。有任何想法嗎?如何檢查已使用意向發送的URI的權限?

Uri uri = (Uri) intent.getExtras().get(Intent.EXTRA_STREAM); 

// check uri permissions but does not work 
int perm = checkUriPermission(uri , "r", null, 
     Binder.getCallingPid(), Binder.getCallingUid(), 
     Intent.FLAG_GRANT_READ_URI_PERMISSION); 
if (perm == PackageManager.PERMISSION_DENIED) { 
    Toast.makeText(getApplicationContext(), 
    "No permissions to read the file", Toast.LENGTH_LONG).show(); 
    finish(); 
} 

回答

3

r不是權限。嘗試傳遞null作爲第二個參數,或使用checkUriPermission()版本跳過第二個和第三個位置中的兩個String參數。

+0

啊,現在我明白了。我總是必須知道我想要檢查的權限。但那不是我想要的。我想知道我是否可以讀取文件。有時候還有其他應用程序允許發送文件,但是我的應用程序沒有讀取它們的權限。所以我必須趕上SecurityException ... – mohlendo 2011-06-11 09:49:25