简体   繁体   中英

How to create a folder “beam” on SDcard (“/ sdcard / beam”)

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" />

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();

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

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();
} 

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

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM