I am trying to create folder on external microsd card but it is not working.
String sdpath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/sdcard/Ankur Jain";
Toast.makeText(Second.this,sdpath,Toast.LENGTH_LONG).show();
File file = new File(sdpath);
if(!file.exists()){file.mkdirs();
Toast.makeText(Second.this,"Created path",Toast.LENGTH_LONG).show();
}
i have added all permission too.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
and build gradle app as below
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "jss.intentgame"
minSdkVersion 11
targetSdkVersion 22
versionCode 1
versionName "1.0"
Now what is stopping to create folder on card?
First, new File()
does not create a file. It creates a File
object. Your question has no code for creating a file or directory.
Second, Environment.getExternalStorageDirectory().getAbsolutePath()+"/sdcard/Ankur Jain"
will not point to "external microsd card" on most, if not all, Android devices. That points to some subdirectory that you are trying to create on external storage .
Third, you do not have arbitrary access to removable storage on Android 4.4+.
If you want to allow the user to work with removable storage, use the Storage Access Framework on Android 4.4+. There was no Android SDK support for removable storage prior to that.
Let me share the basic working code that you can see and try accordingly. Hope this would help you to create the folder.
public class AppConst{
private static String APPLICATION_PATH = Environment.getExternalStorageDirectory().getPath();
private static String DIR_HOME = “YourRootDirName”;
private static String IMAGE_HOME = “SubDirectoryName”;
private static String getApplicationPath() {
return (APPLICATION_PATH + "/");
}
public static String getRootPath() {
return (getApplicationPath() + DIR_HOME + "/");
}
public static String getImageAbsolutePath() {
return getRootPath() + IMAGE_HOME + "/";
}
}
public class StorageManager {
public static void verifyImagePath() {
File imageDirectory = new File(AppConst.getImageAbsolutePath());// It use the path that we define in constant file.
if (!imageDirectory.exists()) {
imageDirectory.mkdir();
}
}
}
}
Now In your application, Find out the place where you want to start creating the folder and write this below code.
StorageManager. verifyImagePath();
The above statement does create the folder on defined location on Storage option.
Note: Make sure you must give the read/write permission in your app Manifest file. If you are running the app in marshmallow device make sure you award the read/write permission to your application.
I got the trick to solve my issues. Get list of storage devices, trim its path and we will get absolute path for sdcard storage. With normal mkdris create folder. Here is code.
File[] externalStorageFiles = ContextCompat.getExternalFilesDirs(MainActivity.this, null);
storagepath = new String[externalStorageFiles.length]; for (int i = 0; i < externalStorageFiles.length; i++) {
storagepath[i] = externalStorageFiles[i].toString();
System.out.println(storagepath[i]);
}
Change your code to getPath() instead of getAbsolutePath(). Working in my case for all the conditions.
Environment.getExternalStorageDirectory().getPath()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.