Creating a directory in /sdcard fails
There are three things to consider here:
Don't assume that the sd card is mounted at
/sdcard
(May be true in the default case, but better not to hard code.). You can get the location of sdcard by querying the system:Environment.getExternalStorageDirectory();
You have to inform Android that your application needs to write to external storage by adding a uses-permission entry in the AndroidManifest.xml file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
If this directory already exists, then mkdir is going to return false. So check for the existence of the directory, and then try creating it if it does not exist.
In your component, use something like:File folder = new File(Environment.getExternalStorageDirectory() + "/map");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
if (success) {
// Do something on success
} else {
// Do something else on failure
}
Unable to create directory: /sdcard/Downloads
secondary storages (like the real removable SD card) are protected by a new permission android.permission.WRITE_MEDIA_STORAGE
, and the protection level is signatureOrSystem. one of the answers at stack overflow suggests this.
check this answer aswell, which conveys :
From API level 19, Google has added API.
Context.getExternalFilesDirs()
Context.getExternalCacheDirs()
Context.getObbDirs().
Related Topics
Android: Android.Content.Res.Resources$Notfoundexception: String Resource Id #0X5
Android Take Screen Shot Programmatically
Creating Custom Lockscreen in Android
Ripple Effect on Android Lollipop Cardview
Error:Failed to Open Zip File. Gradle's Dependency Cache May Be Corrupt
Android Volley - How to Isolate Requests in Another Class
How to Set Textinputlayout Error Message Colour
Phonegap Camera Restarts the Application
A Way to Get Unlock Event in Android
Android - Volume Buttons Used in My Application
Custom Info Window Adapter with Custom Data in Map V2
Geocoder.Getfromlocation Throws Ioexception on Android Emulator
Android Pendingintent Extras, Not Received by Broadcastreceiver
Download the Android Sdk Components for Offline Install