How to create folder in sdcard in android
Asked Answered
C

5

5

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?

Chrestomathy answered 9/1, 2014 at 18:28 Comment(6)
File file = new File(Environment.getExternalStorageDirectory(), path); try this.Rositaroskes
Just creating a File object doesn't actually do anything to the SD-card. If you want to create a directory, you need to call mkdir() on that File object. If you want to create a file, you need to call createNewFile() on that object.Greedy
@RajeshCP I tried, but same result.Chrestomathy
Yeah, I used mkdir() and mkdirs() as you see above in Log.d(...).Chrestomathy
can u check the status of external storage, whether it is writable or not ?Rositaroskes
Isn't it enough if I have the permission and media state mounted?Chrestomathy
C
0

I don't know why the application acted that way, but finally I overcame this problem by removing and resetting the sdcard write permission in the Android manifest file. Thanks all of you for your kind help and apologize for taking your time.

Chrestomathy answered 9/1, 2014 at 19:17 Comment(0)
G
13

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" />
Golconda answered 9/1, 2014 at 18:41 Comment(6)
You see I have done this in my code. I must get to the bottom of the problem.Chrestomathy
Just curious why it wold not be better to use Environment.getExternalStorageDirectory()? Is hard coding workable for all devices and future proof?Bogoch
@KickingLettuce: yes you are right, we should use Environment.getExternalStorageDirectory() but for testing I use /sdcard and its working fine in my all emulators and device. Thanks for suggestion, I edited my answer.Golconda
@AliAllahyar: you can mark this answer as complete by clicking "right" sign below votes if your problem was solved.Golconda
@Golconda You might need to edit your code to java File mydir = new File(Environment.getExternalStorageDirectory(), "/mydir/"); Idolist
@shanwu: both will work, the code you posted and I have in the answer will create a same fileGolconda
P
1

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();
Plugugly answered 9/1, 2014 at 18:35 Comment(2)
I used your approach, new file with two parameters and also your exact code. still no benefit.Chrestomathy
did you try on different phones?Plugugly
N
1

Try this way:

File file=new File(Environment.getExternalStorageDirectory() + File.separator +"MyApp/");
file.mkdirs();
Nudge answered 9/1, 2014 at 18:39 Comment(2)
I still get false in LogCat and I double checked the sdcard as well and no folder was created.Chrestomathy
i have tried.it work properly.i have one application that work same way.Nudge
H
1

Try this code:) , its simple.

        String fileName = "CallHistory.pdf";

        // create a File object for the parent directory
        File PDF_Directory = new File("/sdcard/MOBstate/");

        // have the object build the directory structure, if needed.          
        PDF_Directory.mkdirs();

        // create a File object for the output file           
         File outputFile = new File(PDF_Directory, fileName);

        // now attach the OutputStream to the file object, instead of a ring representation
         FileOutputStream fos = new FileOutputStream(outputFile);
Huldahuldah answered 15/12, 2016 at 9:13 Comment(0)
C
0

I don't know why the application acted that way, but finally I overcame this problem by removing and resetting the sdcard write permission in the Android manifest file. Thanks all of you for your kind help and apologize for taking your time.

Chrestomathy answered 9/1, 2014 at 19:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.