1

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)

2 Answers2

3

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

Karthik Pai
  • 587
  • 1
  • 9
  • 18
  • what doing method "doFurtherProcessing()" ? – Кирилл Балашов Apr 30 '19 at 13:41
  • The method doFurtherProcessing () should be written by you. I have written the code so that the program makes a request to the user to grant permissions for reading and writing to external storage. After user grants it, doFurtherProcessing () method gets called in my program. You have to write all the code that gets processed after recieving permissions in doFurtherProcessing () method. – Karthik Pai May 01 '19 at 08:29
  • very good. thank you very much! – Кирилл Балашов May 01 '19 at 09:38
2

You have to use WRITE_EXTERNAL_STORAGE permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Runtime Permission

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;
}
Aabauser
  • 533
  • 1
  • 4
  • 17