FileNotFoundException open failed: EPERM (Operation not permitted) during saving image file to internal storage on android

We Are Going To Discuss About FileNotFoundException open failed: EPERM (Operation not permitted) during saving image file to internal storage on android. So lets Start this Java Article.

FileNotFoundException open failed: EPERM (Operation not permitted) during saving image file to internal storage on android

  1. FileNotFoundException open failed: EPERM (Operation not permitted) during saving image file to internal storage on android

    As a temporary fix you could do: In your android/app/src/main/AndroidManifest.xml file, at
    <application android:label="APPNAME" android:icon="ICONNAME" ...>
    add android:requestLegacyExternalStorage="true", like so:
    <application android:label="APPNAME" android:icon="ICONNAME" android:requestLegacyExternalStorage="true">

  2. EPERM (Operation not permitted) during saving image file to internal storage on android

    As a temporary fix you could do: In your android/app/src/main/AndroidManifest.xml file, at
    <application android:label="APPNAME" android:icon="ICONNAME" ...>
    add android:requestLegacyExternalStorage="true", like so:
    <application android:label="APPNAME" android:icon="ICONNAME" android:requestLegacyExternalStorage="true">

Solution 1

As a temporary fix you could do: In your android/app/src/main/AndroidManifest.xml file, at

<application android:label="APPNAME" android:icon="ICONNAME" ...>

add android:requestLegacyExternalStorage="true", like so:

<application android:label="APPNAME" android:icon="ICONNAME" android:requestLegacyExternalStorage="true">

It will then use the pre-Android 10 way of requesting access to storage, so you can still save your file as you could earlier.

But caution: After you update your app to target Android 11 (API level 30), the system ignores the requestLegacyExternalStorage attribute when your app is running on Android 11 devices, so your app must be ready to support scoped storage and to migrate app data for users on those devices. See https://developer.android.com/training/data-storage/use-cases#opt-out-scoped-storage for more info

Original Author Of This Content

Solution 2

I got the issue
In android 11 “:” is not allowed in file name.
And when you append date at the end of file name its add “:” at the end.
so just replace all “:” with “.” and its working fine.

        String fileName = 
              System.currentTimeMillis().toString().replaceAll(":", ".") + ".jpg";

Original Author Of This Content

Solution 3

The permission system in android is very strict, you must take this into account when writing to internal memory
Try something like this:

    public class MainActivity extends AppCompatActivity {
   Button save;
   Bitmap bitmap;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Drawable drawable = getResources().getDrawable(R.drawable.mario);
      bitmap = ((BitmapDrawable) drawable).getBitmap();
      save = findViewById(R.id.save);
      save.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            ContextWrapper cw = new ContextWrapper(getApplicationContext());
            File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
            File file = new File(directory, "UniqueFileName" + ".jpg");
            if (!file.exists()) {
               Log.d("path", file.toString());
               FileOutputStream fos = null;
               try {
                  fos = new FileOutputStream(file);
                  bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                  fos.flush();
                  fos.close();
               } catch (java.io.IOException e) {
                  e.printStackTrace();
               }
            }
         }
      });
   }
}

Original Author Of This Content

Solution 4

Add the following attribute in your app’s Manifest.xml file inside the application tag:

android:requestLegacyExternalStorage="true"

Finally, it will look like this:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:requestLegacyExternalStorage="true">
...
</application>

Original Author Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment