如何修复“权限被拒绝”? [复制]
Posted
技术标签:
【中文标题】如何修复“权限被拒绝”? [复制]【英文标题】:How to fix 'Permission denied'? [duplicate] 【发布时间】:2019-09-18 22:08:35 【问题描述】:我正在尝试将我的位图保存到文件中,但 android studio 抛出异常 java.io.FileNotFoundException: /storage/emulated/0/Pictures/savedBitmap.png (Permission denied)
我做错了什么?
public void save()
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "savedBitmap.png");
if (file.exists())
Log.i(TAG, "file is exists");
else
Log.i(TAG, "file is not exists");
try
FileOutputStream fos = null;
try
fos = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
finally
if (fos != null) fos.close();
catch (Exception e)
e.printStackTrace();
2019-04-30 13:03:03.058 22470-22470/com.example.paint I/PaintView: file is not exists
2019-04-30 13:03:03.059 22470-22470/com.example.paint W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Pictures/savedBitmap.png (Permission denied)
【问题讨论】:
您是否为您的 AnroidManifest.xml 添加了 android.permission.WRITE_EXTERNAL_STORAGE 权限? 是的,我有这样的权限 在棉花糖上为 sdk 添加运行时权限 【参考方案1】:您需要先将 android.permission.WRITE_EXTERNAL_STORAGE 和 android.permission.READ_EXTERNAL_STORAGE 权限添加到您的 AndroidManifest.xml。然后,您需要以编程方式检查活动的 onCreate() 方法中是否启用了这些权限。您可以按如下方式进行。
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
List<Integer> lPermission = new ArrayList<>();
List<String> stringPermissionList1 = getPermissionList();
for (int i = 0; i < stringPermissionList1.size(); i++)
lPermission.add(ContextCompat.checkSelfPermission(activity, stringPermissionList1.get(i)));
boolean bPermissionDenied = false;
for (int i = 0; i < lPermission.size(); i++)
int a = lPermission.get(i);
if (PackageManager.PERMISSION_DENIED == a)
bPermissionDenied = true;
break;
if (bPermissionDenied)
String sMessage = "Please allow all permissions shown in upcoming dialog boxes, so that app functions properly";
//make request to the user
List<String> stringPermissionList = getPermissionList();
String[] sPermissions = stringPermissionList.toArray(new String[stringPermissionList.size()]);
//request the permissions
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
requestPermissions(sPermissions, PERMISSION_REQUEST_CODE);
else
doFurtherProcessing();
else
doFurtherProcessing();
private List<String> getPermissionList()
List<String> stringPermissionList=new ArrayList<>();
stringPermissionList.add(Manifest.permission.READ_EXTERNAL_STORAGE);
stringPermissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
return stringPermissionList;
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
boolean isAllPermissionGranted = true;
for (int i = 0; i < grantResults.length; i++)
int iPermission = grantResults[i];
if (iPermission == PackageManager.PERMISSION_DENIED)
isAllPermissionGranted = false;
break;
if (isAllPermissionGranted)
doFurtherProcessing();
else
// Prompt the user to grant all permissions
希望对你有帮助
【讨论】:
方法“doFurtherProcessing()”在做什么? 方法doFurtherProcessing()应该是你自己写的。我已经编写了代码,以便程序向用户发出请求以授予对外部存储的读写权限。在用户授予它之后,doFurtherProcessing () 方法在我的程序中被调用。您必须编写在 doFurtherProcessing() 方法中获得权限后处理的所有代码。 非常好。非常感谢!【参考方案2】:您必须使用 WRITE_EXTERNAL_STORAGE 权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
运行时权限
if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)
Toast.makeText(context, " Allow the Storage Permission", Toast.LENGTH_LONG).show();
ActivityCompat.requestPermissions((Activity) context,
new String[]Manifest.permission.WRITE_EXTERNAL_STORAGE,
MY_PERMISSIONS_CONSTANT);
return false;
【讨论】:
我正在使用 WRITE_EXTERNAL_STORAGE 权限 这段代码有什么作用,在哪里插入? 由于 Marshmallow,Android 需要明确征求用户的许可,无论应用是否应该使用该许可。此类权限称为危险权限。请在 google 上搜索 android 的“危险权限”。以及如何启用它们。以上是关于如何修复“权限被拒绝”? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
如何修复错误:无法打开 laravel.log 权限被拒绝?
如何修复以下 Django 错误:“类型:IOError”“值:[Errno 13] 权限被拒绝”