Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create folder in sdcard in android

I want to make folders in my sdcard and I have used the code below:

public class Screen extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);

        operateOnFirstUsage();
    }

    private void operateOnFirstUsage() {

        String state = Environment.getExternalStorageState();
        Log.d("Media State", state);

        if (Environment.MEDIA_MOUNTED.equals(state)) {
            File appDirectory = new File(
                    Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/");

            Log.d("appDirectroyExist", appDirectory.exists() + "");
            if (!appDirectory.exists()) 
                Log.d("appDir created: ", appDirectory.mkdir() + "");

            File dbDirectory = new File(
                    Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/Database/");

            Log.d("dbDirectroyExist", dbDirectory.exists() + "");
            if (!dbDirectory.exists())
                Log.d("dbDir created: ", dbDirectory.mkdirs() + "");


            File themesDirectory = new File(
                    Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/Themes/");
            Log.d("themesDirectroyExist", themesDirectory.exists() + "");
            if (!themesDirectory.exists()) 
                Log.d("themesDir created: ", themesDirectory.mkdirs() + "");

        }
    }
}

Also, I have set the sdcard write permission:

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

I've run the application several times and every time I get the LogCat output:

01-09 21:38:13.701: D/Media State(15363): mounted
01-09 21:38:13.701: D/appDirectroyExist(15363): false
01-09 21:38:13.701: D/appDir created:(15363): false
01-09 21:38:13.701: D/dbDirectroyExist(15363): false
01-09 21:38:13.701: D/dbDir created:(15363): false
01-09 21:38:13.701: D/themesDirectroyExist(15363): false
01-09 21:38:13.701: D/themesDir created:(15363): false

I have read similar question, but nothing useful to get. What should I do to get the code running? What is my problem?

like image 646
Ali Allahyar Avatar asked Jan 09 '14 18:01

Ali Allahyar


People also ask

How do I create a folder on my memory card?

What to Know. Go to My Files > Internal Storage > folder > Menu > Edit > pick files > Move > SD Card > Create Folder > Done.

How do I move Apps to my SD card?

From your Home screen, tap the Application screen icon. Find and tap Settings → Apps. Tap the On SD card tab. Select application, then tap Move to SD card.


2 Answers

Edited

Try this:

File mydir = new File(Environment.getExternalStorageDirectory() + "/mydir/");
if(!mydir.exists())
    mydir.mkdirs();
else
    Log.d("error", "dir. already exists");

And recheck permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
like image 149
dakshbhatt21 Avatar answered Sep 25 '22 14:09

dakshbhatt21


This is how I created folder MyDir in the Pictures folder od sdcard:

File mediaStorageDir =  new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"MyDir");
mediaStorageDir.mkdirs();
like image 22
gile Avatar answered Sep 22 '22 14:09

gile