I want to create a file(not created) in a directory(not created) in the SDCARD. How doing it ?
Thank you.
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.
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.
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" />
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.
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With