1
votes

I want to choose image from sdcard and encrypt them. i have some problem with java io exception - permission denied. i declared use permission in android manifest READ and WRITE EXTERNAL STORAGE but it doesn't work

enter image description here

then I open Device File Explorer and check permission, all of them don't have permission write

my code:

File file = new File(uri_result);//create path from uri
            final String[] split = file.getPath().split(":");//split the path.
            String filePath = split[2];

            try {
                FileInputStream fis = new FileInputStream(filePath);
                File fos_enc = new File( Environment.getExternalStorageDirectory().getAbsolutePath()+"/abcd.jpg");
                fos_enc.createNewFile();
                FileOutputStream fos = new FileOutputStream(fos_enc);

                byte[] k = "123456".getBytes();
                SecretKeySpec key = new SecretKeySpec(k,"DES");
                Cipher enc = Cipher.getInstance("DES");
                enc.init(Cipher.ENCRYPT_MODE, key);
                CipherOutputStream cos = new CipherOutputStream(fos, enc);
                byte[] buf = new byte[1024];
                int read;
                while ((read = fis.read(buf)) != -1) {
                    cos.write(buf,0,read);
                }
                fis.close();
                fos.flush();
                cos.close();
                showAlertDialog("Encrypt Successfully");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

permission denied because the sdcard don't access write the file and i have no idea to resolve this. Hepl me please :)
fos_enc=newFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/abcd.jpg");

3
From API 23 and later you need to ask for permissions in runtime as well.Yupi
Please read Request App Permission.Enzokie

3 Answers

2
votes

You need to add permission for it. Because you need external file read-write permission

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

You need to request permission in runtime before doing that operation like below (this is needed for devices which use lollipop and above android os)

if (ContextCompat.checkSelfPermission(thisActivity,Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {
    // Permission is not granted
     if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.
    } else {
        // No explanation needed; request the permission
        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE);
    }
}
0
votes

Work with external storage requires to permission from user. You need to ask it from user in runtime. Check this doc. Or access permission manually in settings of application for testing.

0
votes

android:requestLegacyExternalStorage="true" fixed my problem