0

I tried a lot to find a solution but everything has failed so far. It's probably something stupid.

I'm trying to save a photo as a jpeg in the storage of the phone. Everything fails even the simple task of making a folder. The code below is what I use to create the folder.

private void makeFolder(){
    try{
        File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),getString(R.string.app_name));
        boolean success = true;
        if (!folder.exists()) {
            success = folder.mkdirs();
        }
        if (success) {
            Toast.makeText(this,"Done",Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this,"folder_failed",Toast.LENGTH_SHORT).show();
            System.out.println(folder.getAbsolutePath());
        }
    }catch (Exception e){
        Toast.makeText(this,"exception",Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }
}

I have tried many different ways like:

        File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
                File.separator + getString(R.string.app_name)+File.separator);

        File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
                File.separator + getString(R.string.app_name);
        
        File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
                ,getString(R.string.app_name));

I have tried both mkdirs and mkdir and

-Environment.getExternalStorageDirectory().getAbsolutePath()
-getExternalStorageDirectory().getAbsolutePath()
-Environment.getExternalStorageDirectory()
-getExternalStorageDirectory()

The permission exists in the manifest and is accepted.

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

The path the the System.out prints is "/storage/emulated/0/appname"

I don't know what else to try. Similar code works for other applications. Min API is 23. Any help is much appreciated.

mremremre1
  • 1,036
  • 3
  • 16
  • 34
  • what about READ_EXTERNAL_STORAGE? Do you get any kind of error/exception? – Steyrix Jul 28 '20 at 09:56
  • Whats your Android version from the test device? Android 10/Q? – Felix Jul 28 '20 at 09:59
  • READ_EXTERNAL_STORAGE also exists. The code above does not throw any exceptions. When I try to later create a file using ".createNewFile();" it does throw an exception "File not found". But the folder creation fails without exception – mremremre1 Jul 28 '20 at 10:00
  • 1
    If you trying to create in API level 29+ then it's not working. Check this answer @mremremre1 **[Can't create directory in Android 10](https://stackoverflow.com/questions/58379543/cant-create-directory-in-android-10)** – Ali Jul 28 '20 at 10:06

2 Answers2

1

I suggest you to try Context.getExternalFilesDir() instead of Environment.getExternalStorageDirectory()

Steyrix
  • 2,796
  • 1
  • 9
  • 23
  • This works. Thank you. It creates the folder inside Android/data/"app path"/myfolder. This is fine for me. Is it no longer supported making files in the root directory? – mremremre1 Jul 28 '20 at 10:07
  • @mremremre1 I am glad this worked, hope it allows you to achieve the right logic. Also note, that `getExternalStorageDirectory` is deprecated and does not work on some of the devices – Steyrix Jul 28 '20 at 10:10
1

If your test device runs Android 10/Q or API Level 29+: there are a few permission changes with storing data to media folder. You can store your data without problems to your app folder, but if you remove the app the images are gone too.

The easiest way is to add android:requestLegacyExternalStorage="true" in the manifest under application but it is more like a work around and it will probably not be supported for long.

Search for "store image android 10" here and you find the right way for you.

Felix
  • 284
  • 2
  • 12