1

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

letsintegreat
  • 3,328
  • 4
  • 18
  • 39
Tejas
  • 23
  • 8
  • `getSdCardPaths` A strange function name as there can be only one removable micro sd card inserted as far as a met devices. – blackapps Jan 29 '21 at 11:51
  • @blackapps good observation, but there can be more than one card, and here I am getting internal and external both path – Tejas Jan 29 '21 at 12:29
  • Yes there can be more paths. But only one path for an sd card. – blackapps Jan 29 '21 at 12:59

2 Answers2

1

Removable micro SD cards are read only since Android 4 Kitkat.

You only have write access in one app specific directory in the Android folder on the card.

blackapps
  • 8,011
  • 2
  • 11
  • 25
  • thanks for reply. But some apps like Xender are creating folder at root, can you explain how they can create ? – Tejas Jan 29 '21 at 12:00
  • They probably use SAF: Storage Access Framework. Intent.ACTION_OPEN_DOCUMENT_TREE for example and then the user creates the directory or the app does. – blackapps Jan 29 '21 at 12:02
  • Thank you, will check about that – Tejas Jan 29 '21 at 12:30
0

Probably the Permission issue. On and above Android 10 you are restricted to have write permission on SD card.

See the official doc for more details.

https://developer.android.com/training/data-storage#permissions

SRB Bans
  • 3,096
  • 1
  • 10
  • 21