提问者:小点点

如何修复“权限被拒绝”?[复制]


我正试图将位图保存到文件中,但android studio抛出了一个异常java。io。FileNotFoundException:/storage/simulated/0/Pictures/savedBitmap。png(权限被拒绝)我做错了什么?

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)

共2个答案

匿名用户

您需要首先添加android。准许写入外部存储和android。准许AndroidManifest的READ\u EXTERNAL\u存储权限。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
        }

}

希望这能有所帮助

匿名用户

你得用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;
}