0

I need to create a folder "BEAM" on the SD card, but it says to me that the folder cannot be created.

I used this code:

    File mediaDir = new File("/sdcard/beam");
        / / Create a folder if not exists
        if (!mediaDir.exists()) {
            mediaDir.mkdir(); // this code return false
        }

Is folder "beam" protected?

I have also this Android permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Ariel Magbanua
  • 3,083
  • 7
  • 37
  • 48

5 Answers5

0

Try below code. Do not use hardcore string like 'sdcard' or 'mnt/sdcard'

File f = new File(android.os.Environment.getExternalStorageDirectory(),File.separator+"beam/");
f.mkdirs();
Chirag
  • 56,621
  • 29
  • 151
  • 198
0

It is not always at the same location, try using

Environment.getExternalStorageState() returns path to internal SD mount point like "/mnt/sdcard"

For example my phone location would be /storage/sdcard0/

Hope this helps

Xavjer
  • 8,838
  • 2
  • 22
  • 42
0

Refer below code

 if (android.os.Environment.getExternalStorageState().equals(
                 android.os.Environment.MEDIA_MOUNTED)) {

                 File f = new File(
                         Environment.getExternalStorageDirectory() + File.separator + "beam");
                 f.mkdirs();
} 
Nirali
  • 13,571
  • 6
  • 40
  • 53
0

Have you tried to create the folder using 'adb shell' and then 'mkdir /sdcard/beam'? It should work fine. If it's not, you should get and idea, what's wrong. Also, don't hardcode sdcard folder. See:

Creating a directory in /sdcard fails

Android mkdir not making folder

Community
  • 1
  • 1
Zielony
  • 16,239
  • 6
  • 34
  • 39
0

Your code is correct, only you are missing a / after beam word, add it and try again like following code,

File mediaDir = new File("/sdcard/beam/");  // / is added after beam
// Create a folder if not exists
if (!mediaDir.exists()) 
{
     mediaDir.mkdir(); // this code return false
}
Raynold
  • 443
  • 2
  • 9
  • 28