How to fix 'Permission denied'?

Кирилл Балашов

I'm trying to save my bitmap to a file, but the android studio throws an exception java.io.FileNotFoundException: /storage/emulated/0/Pictures/savedBitmap.png (Permission denied) What am I doing wrong?

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)
Karthik Pai

You need to first add android.permission.WRITE_EXTERNAL_STORAGE and android.permission.READ_EXTERNAL_STORAGE permission to your AndroidManifest.xml. Then programmaticaly you need to check whether these permissions are enabled in your activity's onCreate() method. You can do it as follows.

@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
        }

}

Hope this helps

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to fix Permission Denied (Public key) error?

How to fix docker: Got permission denied issue

How to fix [Errno 13] Permission denied with openpyxl

How do I fix ngrok permission denied?

How to fix 'Permission denied' in Docker sh entrypoint

How to fix amvavis reporting "permission denied" for clamav

How to fix 'Permission Denied' when using csshX

How to fix Permission denied (publickey,password) in gitlab Ci/CD?

How to fix gradlew permission denied in travis.yml?

How to fix Error: mkdir(): Permission denied when running composer

How to fix Error: laravel.log could not be opened Permission denied?

How to fix permission denied for .git/ directory when performing git push?

How to fix “Permission denied” when trying to edit a file on aws server?

How can I fix "PermissionError: [Errno 13] Permission denied: 'static'"?

How to fix "Permission Denied" in Git Bash in Windows 10?

How to fix `listen EACCES: permission denied` on any port

How to fix psutil import error "permission denied" on linux?

How to fix Nginx connection to socket failed (Permission denied)

How to fix permission being denied when installing Angular Client on MacOS?

How do I fix the npm error EACCES: permission denied?

How do I Fix "Permission Denied" Error in Python

How do I fix problem with getting "permission denied" when Flask server is saving an image file

How can I fix "[Errno 13] Permission denied: '_cmp.pyi'" in my Python nix flake?

How to fix denied permission to access a directory if that directory was added during docker build?

gitlab: Windows: How to use chmod and fix "Get Permission denied (publickey). fatal: Could not read from remote repository"

How to fix " Error: Permission denied @ [file directory here]" while 'limking' python 3 on Mac

How to fix [Errno13] permission denied when trying to read excel file?

How do I fix this "Permission denied" error I get every time I execute aptitude?

How to fix" Permission denied when creating a hive orc table using spark "?