Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a file in an SDCARD directory

I want to create a file(not created) in a directory(not created) in the SDCARD. How doing it ?

Thank you.

like image 327
androniennn Avatar asked Apr 11 '11 17:04

androniennn


People also ask

How do I create a folder on Android SD card?

Go to My Files > Internal Storage > folder > Menu > Edit > pick files > Move > SD Card > Create Folder > Done. To move an app, go to Settings > Apps > select app > Storage > Change > SD Card.

Can you make folders on an SD card?

Tap SD card. If you want to move or copy files to a new folder: Choose where you want to create a new folder, then tap Add new folder. Enter folder name in the pop-up.


3 Answers

Try the following example:

if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
    //handle case of no SDCARD present
} else {
    String dir = Environment.getExternalStorageDirectory()+File.separator+"myDirectory";
    //create folder
    File folder = new File(dir); //folder name
    folder.mkdirs();

    //create file
    File file = new File(dir, "filename.extension");
}

Don't forget to add the permission to your AndroidManifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
like image 103
Will Tate Avatar answered Oct 20 '22 16:10

Will Tate


The problem is that mkdirs() is called on a File object containing the whole path up to the actual file. It should be called on a File object containing the Path (the directory) and only that. Then you should use another File object to create the actual file.

like image 43
biril Avatar answered Oct 20 '22 14:10

biril


You should also have to add permission to write to external media. Add following line in the application manifest file, somewhere between <manifest> tags, but not inside <application> tag:

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

like image 2
uaraven Avatar answered Oct 20 '22 15:10

uaraven