Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a folder in android External Storage Directory?

I cannot create a folder in android External Storage Directory.

I have added permissing on manifest,

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

Here is my code:

 String Path = Environment.getExternalStorageDirectory().getPath().toString()+ "/Shidhin/ShidhiImages";   System.out.println("Path  : " +Path );   File FPath = new File(Path);   if (!FPath.exists()) {         if (!FPath.mkdir()) {             System.out.println("***Problem creating Image folder " +Path );         }   } 
like image 206
SHIDHIN.T.S Avatar asked Jul 16 '14 12:07

SHIDHIN.T.S


People also ask

What is external storage path in Android?

Android External Storage Example Code getExternalFilesDir(): It returns the path to files folder inside Android/data/data/application_package/ on the SD card. It is used to store any required files for your app (like images downloaded from web or cache files).

How can I write to external storage in Android?

To read and write data to external storage, the app required WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE system permission. These permissions are added to the AndroidManifest. xml file. Add these permissions just after the package name.

How do I create a folder in Android root directory?

Show activity on this post. File mydir = context. getDir("mydirectory", Context. MODE_PRIVATE); //Creating an internal dir; File fileWithinMyDir = new File(mydir, "myAwesomeFile"); //Getting a file within the dir.


2 Answers

Do it like this :

String folder_main = "NewFolder";  File f = new File(Environment.getExternalStorageDirectory(), folder_main); if (!f.exists()) {     f.mkdirs(); } 

If you wanna create another folder into that :

File f1 = new File(Environment.getExternalStorageDirectory() + "/" + folder_main, "product1"); if (!f1.exists()) {     f1.mkdirs(); } 
like image 53
Dhaval Avatar answered Sep 16 '22 22:09

Dhaval


The difference between mkdir and mkdirs is that mkdir does not create nonexistent parent directory, while mkdirs does, so if Shidhin does not exist, mkdir will fail. Also, mkdir and mkdirs returns true only if the directory was created. If the directory already exists they return false

like image 20
Blackbelt Avatar answered Sep 17 '22 22:09

Blackbelt