0

I am new to Xamarin Android, but have some experience with C# and WinForms. I have a modern Android device using Android 9. I can use the built-in File Manager to see my SD card. I can also use that app to create a folder on my SD card (for example "Test Folder"). What I have not been able to do is access that folder using my C# Xamarin Android code. Should I be able to, or is this frowned upon? Is there a generic solution that will work on other devices that also have an SD card?

The Android.OS.Environment.GetExternalStoragePublicDirectory method does not seem to recognize the presence of the removable, external storage. I have also tried

new Intent(Intent.ActionOpenDocumentTree);

This allows me to "see" the new folder, but I can't seem to do anything with the URI that I get back.

1 Answers1

0

To create Folder in External Storage (if External Storage is available otherwise it creates in internal storage)

public void createMainFolder (){
    String myfolderLoc=Environment.getExternalStorageDirectory()+"/"+yourFolderName;
    File f=new File(myfolderLoc);
    if(!f.exists()) {
        if (!f.mkdir()) {
           //folder can't be created 

        } else {
           //folder has been created
        }
    }
}

And to get the path of the folder :

   public String getAppmainFolderName(){
 return Environment.getExternalStorageDirectory()+"/"+yourFolderName;
}

Don't forget to add Storage Permission in your manifest.xml

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

and check Storage Permission is granted or not before accessing your External Storage (required for android 6.0 and above).

Request App Permission Android Doc.

Hope this will help you.

Samir Dangal
  • 2,591
  • 2
  • 15
  • 17
  • Thanks for this, but it does not help. The issue is that Environment.getExternalStorageDirectory() does NOT return the root of the SD card. It returns the root of whatever the OS decides is "external storage" and this is not the SD card on many newer devices. – Glassmonkeycode Aug 15 '19 at 20:54
  • Environment.getExternalStorageDirectory() , which pointed to the root of Primary External Storage. Have a look here for more details. https://androidpedia.net/en/tutorial/150/storing-files-in-internal-external-storage – Samir Dangal Aug 21 '19 at 04:15
  • Hope this tutorial will help you. :) – Samir Dangal Aug 21 '19 at 04:17