Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.io.FileNotFoundException: open failed: EACCES (Permission denied)

I got this error when I trying to storage a bitmap into storage #

    File path = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "picture");
    if (! path.exists()) {
        path.mkdirs();
        if (!path.exists()) {
            return null;
        }
    }
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HH_mm_ss", Locale.CHINA).format(new Date());
    File imagePath = new File(path.getPath() + "_" + "IMG_" + timeStamp + ".jpg");
    BufferedOutputStream fos;
    try {
        fos =new BufferedOutputStream(new FileOutputStream(imagePath));
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
        return imagePath;
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
        return null;
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
        return null;
    }

fos = new BufferedOutputStream(new FileOutputStream(imagePath));

I debug and found this line cause the error.

And in manifest the permission set is right

like image 881
Alexander Avatar asked Sep 11 '15 12:09

Alexander


3 Answers

This issue is in Android Pie and higher version. So, adding this line in manifest file fixed error.

<application
    ...
    ...
    android:requestLegacyExternalStorage="true">
</application>
like image 85
F_Z Avatar answered Nov 14 '22 21:11

F_Z


on Android10

use method -> Environment.getExternalStoragePublicDirectory()

java.io.FileNotFoundException: /storage/emulated/0/Download open failed: EACCES (Permission denied)

Before your app is fully compatible with scoped storage, you can temporarily opt out based on your app's target SDK level or the requestLegacyExternalStorage manifest attribute:

Google has a new feature on Android Q: filtered view for external storage. A quick fix for that is to add this code in the AndroidManifest.xml file:


<manifest ... >
  <!-- This attribute is "false" by default on apps targeting
       Android 10 or higher. -->
  <application android:requestLegacyExternalStorage="true" ... >
    ...
  </application>
</manifest>
like image 25
Mahmoud Zaher Avatar answered Nov 14 '22 23:11

Mahmoud Zaher


The problem is my permission format was wrong

Uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE

The android.permission should be lowercase

uses-permission android:name=android.permission.WRITE_EXTERNAL_STORAGE

and if I add imagePath.createNewFile();

it's also throws FileNotFoundExceptions.

like image 4
Alexander Avatar answered Nov 14 '22 23:11

Alexander