How to open specific folder in android?

aluzi liu 486 Reputation points
2022-07-19T10:42:32.33+00:00

My app will download files from server and store it in specific public folder(for example:/storage/emulated/0/Documents/MyFolder)
I want to open this folder programmatically, I try this code:
(just open it, I don't need choose file and return, just like the user manually tap the system build-in file browser)

            Intent intent = new Intent(Intent.ActionView);  
            Android.Net.Uri path = Android.Net.Uri.Parse("/storage/emulated/0/Documents/MyFolder/");  
            intent.SetDataAndType(path, "*/*");  
            MainActivity.Instance.StartActivity(intent);  
  

But it can not open the folder I wanted.......

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,262 questions
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 31,081 Reputation points Microsoft Vendor
    2022-07-20T06:16:19.003+00:00

    Hello,

    Referring to this documentation Open files using storage access framework, we could find the follow introduction:

    Android 4.4 (API level 19) introduces the Storage Access Framework (SAF). The SAF makes it simple for users to browse and open documents, images, and other files across all of their preferred document storage providers. A standard, easy-to-use UI lets users browse files and access recents in a consistent way across apps and providers.

    Therefore, it's recommended to open files via SAF.

    You could try the following code to use SAF in the OpenFolder() method:

       public void OpenFolder()  
       {  
           StorageManager sm = (StorageManager)MainActivity.Instance.GetSystemService(Context.StorageService);  
           Intent intent = sm.PrimaryStorageVolume.CreateOpenDocumentTreeIntent();  
           String startDir = "Documents"; // You could change startDir to open other dirs, such as DCIM, Downloads...  
           var uri1 = intent.GetParcelableExtra("android.provider.extra.INITIAL_URI");  
           String scheme = uri1.ToString();  
           scheme = scheme.Replace("/root/", "/document/");  
           scheme += "%3A" + startDir;  
           var uri = Uri.Parse(scheme);  
           intent.PutExtra("android.provider.extra.INITIAL_URI", uri);  
           MainActivity.Instance.StartActivityForResult(intent, 200);  
       }  
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful