Getting all the time "permission denied" or "no such file or directory" by trying to save Bitmap image. What should i do?
Asked Answered
S

1

-1

I`m trying to save Bitmap image by this code:

File sdcard = Environment.getExternalStorageDirectory();
            String filename = "test";
            File folder = new File(sdcard, "/Download");
            Log.v("ImageStorage1", "EXiST?: " + folder.exists());
            folder.mkdirs();
            Log.v("ImageStorage2", "EXIST!: " + folder.exists());
            Log.v("ImageStorage", "Folder: " + folder);
            File file = new File(folder, filename + ".jpg");

            try {
                FileOutputStream out = new FileOutputStream(file.getAbsoluteFile());
                result.compress(Bitmap.CompressFormat.JPEG, 90, out);
                out.flush();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

I`m also using in manifests file:

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

But i`m getting this one:

V/ImageStorage1: EXiST?: true
V/ImageStorage2: EXIST!: true
W/System.err: java.io.FileNotFoundException: 
/storage/emulated/0/Download/test.jpg (Permission denied)
    W/System.err:     at java.io.FileOutputStream.open0(Native Method)
    W/System.err:     at java.io.FileOutputStream.open(FileOutputStream.java:287)
    W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:223)
    W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:171)

Actually, my task is to store to another folder and when I`m using this:

File folder = new File(sdcard, "/kpi/test/a");

I`m getting

V/ImageStorage1: EXiST?: false
V/ImageStorage2: EXIST!: false
(No such file or directory)

Even with:

folder.mkdirs();

I tried a lot and surfed a lot, but haven`t found an answer :(

Shadrach answered 9/3, 2018 at 19:40 Comment(9)
did you test it in real phone?Campanology
what is your target version?is it above 23? if so implement run time permisionsCampanology
@ALTegani, yes, I`m trying it on real phone and emulator :(Shadrach
@ALTegani, yes, its 26, dont know anything about runtime permission, so I`ll try to surf and do itShadrach
you should implement runtime permisson @Han vSoloCampanology
let me give you agood way for that ok?@Han vSoloCampanology
@ALTegani, You would be a hero for me, if u help me with this :DShadrach
yes just wait my post @Han vSoloCampanology
if ok with you mark the answer as correctCampanology
C
5

runtime permissions letting user to allow or deny any permission at runtime. use this lib Dexter library.also check an working exmple here

Include the library in your build.gradle

dependencies{
    implementation 'com.karumi:dexter:4.2.0'
}

this example requests WRITE_EXTERNAL_STORAGE.

Dexter.withActivity(this)
                .withPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .withListener(new PermissionListener() {
                    @Override
                    public void onPermissionGranted(PermissionGrantedResponse response) {
                        // permission is granted, open the camera
                    }

                    @Override
                    public void onPermissionDenied(PermissionDeniedResponse response) {
                        // check for permanent denial of permission
                        if (response.isPermanentlyDenied()) {
                            // navigate user to app settings
                        }
                    }

                    @Override
                    public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
                        token.continuePermissionRequest();
                    }
                }).check();

Requesting Multiple Permissions To request multiple permissions at the same time, you can use withPermissions() method. Below code requests STORAGE and LOCATION permissions.

Dexter.withActivity(this)
                .withPermissions(
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.ACCESS_FINE_LOCATION)
                .withListener(new MultiplePermissionsListener() {
                    @Override
                    public void onPermissionsChecked(MultiplePermissionsReport report) {
                        // check if all permissions are granted
                        if (report.areAllPermissionsGranted()) {
                            // do you work now
                        }

                        // check for permanent denial of any permission
                        if (report.isAnyPermissionPermanentlyDenied()) {
                            // permission is denied permenantly, navigate user to app settings
                        }
                    }

                    @Override
                    public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
                        token.continuePermissionRequest();
                    }
                })
                .onSameThread()
                .check();
Campanology answered 9/3, 2018 at 20:6 Comment(1)
You are wellcomeCampanology

© 2022 - 2024 — McMap. All rights reserved.