I am able to find the path of SD card and is mounted as well as, I can create a folder to cache directory (sdcard/Android/data/packagename) but I am not able to create a folder at the root of SD card
My code is like : To get sd card
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static List<String> getSdCardPaths(final Context context, final boolean includePrimaryExternalStorage)
{
final File[] externalCacheDirs=ContextCompat.getExternalFilesDirs(context,null);
if(externalCacheDirs==null||externalCacheDirs.length==0)
return null;
if(externalCacheDirs.length==1)
{
if(externalCacheDirs[0]==null)
return null;
final String storageState= EnvironmentCompat.getStorageState(externalCacheDirs[0]);
if(!Environment.MEDIA_MOUNTED.equals(storageState))
return null;
if(!includePrimaryExternalStorage&& Build.VERSION.SDK_INT>= Build.VERSION_CODES.HONEYCOMB&&Environment.isExternalStorageEmulated())
return null;
}
final List<String> result=new ArrayList<>();
if(includePrimaryExternalStorage||externalCacheDirs.length==1)
result.add(getRootOfInnerSdCardFolder(externalCacheDirs[0]));
for(int i=1;i<externalCacheDirs.length;++i)
{
final File file=externalCacheDirs[i];
if(file==null)
continue;
Log.e("getsdcard",file.getAbsolutePath());
final String storageState=EnvironmentCompat.getStorageState(file);
if(Environment.MEDIA_MOUNTED.equals(storageState))
result.add(getRootOfInnerSdCardFolder(externalCacheDirs[i]));
}
if(result.isEmpty())
return null;
return result;
}
/** Given any file/folder inside an sd card, this will return the path of the sd card */
private static String getRootOfInnerSdCardFolder(File file)
{
if(file==null)
return null;
final long totalSpace=file.getTotalSpace();
while(true)
{
final File parentFile=file.getParentFile();
if(parentFile==null||parentFile.getTotalSpace()!=totalSpace||!parentFile.canRead())
return file.getAbsolutePath();
file=parentFile;
}
}
Now I am getting the path of internal and sd card both and then I am creating a folder like this:
List<String> list=getSdCardPaths(HomeActivity.this,true);
for (int i = 0; i <list.size() ; i++) {
Log.e("list",list.get(i));
File root=new File(list.get(i));
File file=new File(root.getAbsolutePath(), "/Tejas" );
if(!file.exists())
{
Log.e("create", file.mkdirs()+ "");
}
}
This is working in internal storage but it returns false in sd card.
I can create folder in cache directory of sd card, I want to create at root of sd card
All permissions code is done