9

I am trying to create a file. (testipfs folder already exists)

File imagesFolder = new File(Environment.getExternalStorageDirectory(), "testipfs");
File photo= new File(imagesFolder, "desktopWallpaper.jpg");
System.out.println("Saving file in "+photo.getAbsolutePath());
if (!photo.exists()) {
   try {
      photo.createNewFile();
   } catch (IOException e) {
      System.out.println("Failed to create photo");
      e.printStackTrace();
   }
}

But getting error Operation not permitted.

I/System.out: Saving file in /storage/emulated/0/testipfs/desktopWallpaper.jpg
I/System.out: Failed to create photo
W/System.err: java.io.IOException: Operation not permitted
W/System.err:     at java.io.UnixFileSystem.createFileExclusively0(Native Method)
        at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:317)
        at java.io.File.createNewFile(File.java:1008)
        at com.example.ipfs.MainActivity.SavePhotoTask(MainActivity.java:49)
        at com.example.ipfs.MainActivity.onCreate(MainActivity.java:112)

I have added permission in my manifest file as bellow

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

How do i solve this problem?

mahfuj asif
  • 1,691
  • 1
  • 11
  • 32
  • Firstly if you didn't check the permission in runtime use this: `public boolean isExternalStorageWritable(){ String state= Environment.getExternalStorageState(); if(Environment.MEDIA_MOUNTED.equals(state)){ return true; } return false; }` It might be about permissions or maybe that file is created and you overwrite it. – Ahmet Korkmaz Mar 28 '21 at 11:03
  • @AhmetKorkmaz this method returns true. So i do have permission. The file does not exists, i am not overwriting it, i am trying to create. Even if so why i can not overwrite it? – mahfuj asif Mar 29 '21 at 04:11

5 Answers5

10

I am expecting you are testing your application in android 11, In Android 11 things changed ... You have to ask for this permission

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

and in you MainActivity

if (Build.VERSION.SDK_INT >= 30){
        if (!Environment.isExternalStorageManager()){
            Intent getpermission = new Intent();
            getpermission.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
            startActivity(getpermission);
        }
    }

Basically this is just an intent which will take user to setting to give us Permission, and this is necessary in android 11 , If you just asking for READ nd WRITE permission then you application is having just a read-only access to files in the device

Wagonwolf
  • 124
  • 1
  • 2
5

Try to add requestLegacyExternalStorage in Manifest.xml file, if you are using Android 10 or later versions.

<application
    ...
    android:requestLegacyExternalStorage="true">
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
1

Use this

File imagesFolder = new File(Environment.getExternalStoragePublicDirectory(
     Environment.DIRECTORY_DOWNLOADS), "testipfs");

File photo = new File(imagesFolder, "desktopWallpaper.jpg");
Sven Eberth
  • 3,057
  • 12
  • 24
  • 29
1

Declaring permissions in the Manifest file is not enough if you are using Android Marshmallow and above, before you write or read any external files you need to request the appropriate permissions explicitly.

storage-permission-error-in-marshmallow

Documentation

  • If this doesn't work try setting your targetSdkVersion to 28 or less.
Houssem Nasri
  • 90
  • 1
  • 10
1

I had special characters in my directory name (specifically ":"). Removing this allowed me to save the file

Fletch
  • 769
  • 1
  • 12
  • 33