Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make directory in android

Tags:

java

android

Im trying to build a directory called "images" on the SD card on android. This is my code but its not working? Can anyone give me some advice?

File picDirectory = new File("mnt/sdcard/images");
picDirectory.mkdirs();
like image 280
Peter Avatar asked Mar 12 '11 01:03

Peter


People also ask

What is mkdir in Android?

mkdir() Creates the directory named by this abstract pathname. boolean. mkdirs() Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.


4 Answers

Update: Since Android 10,11 Storage updates, Google has restricted Storage access through standard programming language file operations.

For applications targeting only Android 10 (API 29) and above, you need to declare "requestLegacyExternalStorage="true" " in your android manifest file to use programming language based file operations.

<application android:requestLegacyExternalStorage="true" ....>

==========

You want to be sure you are correctly finding the address of your SDCard, you can't be sure its always at any particular address. You will want to do the following!

File directory = new File(Environment.getExternalStorageDirectory()+File.separator+"images");
directory.mkdirs();

Let me know if this works for you!

You will also need the following line in your AndroidManifest.xml

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

like image 54
Will Tate Avatar answered Oct 17 '22 17:10

Will Tate


I use this to know the result:

File yourAppDir = new File(Environment.getExternalStorageDirectory()+File.separator+"yourAppDir");

    if(!yourAppDir.exists() && !yourAppDir.isDirectory()) 
    {
        // create empty directory
        if (yourAppDir.mkdirs())
        {
            Log.i("CreateDir","App dir created");
        }
        else
        {
            Log.w("CreateDir","Unable to create app dir!");
        }
    }
    else
    {
        Log.i("CreateDir","App dir already exists");
    }
like image 32
Adan Avatar answered Oct 17 '22 19:10

Adan


you can use this :

File directory = new File(Environment.getExternalStorageDirectory() + "/images");
directory.mkdirs();
like image 4
Mohsen Bahman Avatar answered Oct 17 '22 19:10

Mohsen Bahman


Environment.getExternalStorageDirectory() is deprecated. So you should use this:

File directory = new File(this.getExternalFilesDir(null).getAbsolutePath() + "/YourDirectoryName");
directory.mkdirs();
like image 3
Kuvonchbek Yakubov Avatar answered Oct 17 '22 18:10

Kuvonchbek Yakubov