java.io.FileNotFoundException: Permission denied 錯誤解決方法
阿新 • • 發佈:2019-01-23
在android6.0以後,需要runtime許可權:requestPermissions方法。
具體如下:
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
//獲取動態許可權
int hasWriteContactsPermission = ContextCompat.checkSelfPermission (MainActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE);//android.manifest.xml
if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
if (!ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission .READ_EXTERNAL_STORAGE)) {
Log.d("yx","get permission");
ActivityCompat.requestPermissions(MainActivity.this,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE);
}
Log.d("yx","get permission2");
ActivityCompat.requestPermissions (MainActivity.this,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE);
}
Log.d("yx","wait for PERMISSION_GRANTED");
while (( ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE))!= PackageManager.PERMISSION_GRANTED) {
}
Log.d("yx","wait for PERMISSION_GRANTED finish");
還需要加入android-support-v4.jar庫。就可以或許android6.0以上的SD卡許可權了。