Android 11 external storage access

Lou, Kecheng 60 Reputation points
2023-06-27T13:40:24.3266667+00:00

Hi Yonglun,

I have two questions below;

#1 For Android version 11, is it possible to access /storage/emulated/0/ folder, and if yes, how to do it?

#2 For Android version 11, is it possible to access some public storage, and how to do it?

If it is possible, can you send the code?

many thanks!

Kevin

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,299 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,443 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 36,871 Reputation points Microsoft Vendor
    2023-06-28T06:06:06.2933333+00:00

    Hello,

    #1 For Android version 11, is it possible to access /storage/emulated/0/ folder, and if yes, how to do it?

    Android provides a special permission for this situation MANAGE_EXTERNAL_STORAGE to manage all files. However, if you use this dangerous permission, your program will not be able to publish to Google play.

    Please refer to Manage all files on a storage device for more details.

    #2 For Android version 11, is it possible to access some public storage, and how to do it?

    For the use of public storage, Android provides two sets of access frameworks, when storing pictures, videos and other media files, Android provides MediaStore to CRUD operations on media files.

    You could refe to the following code sample and documentation for more details.

    // In this code example, by using MediaStore, you can save a picture to the relativePath folder under the album.
    if (Build.VERSION.SdkInt >= BuildVersionCodes.Q)
    {
    	var contentValues = new ContentValues();
    	contentValues.Put(MediaStore.IMediaColumns.DisplayName, "test.png");
    	contentValues.Put(MediaStore.Files.IFileColumns.MimeType, "image/png");
    	contentValues.Put(MediaStore.IMediaColumns.RelativePath, "Pictures/relativePath");
    	try
    	{
    		var uri = MainActivity.Instance.ContentResolver.Insert(MediaStore.Images.Media.ExternalContentUri, contentValues);
    		var output = MainActivity.Instance.ContentResolver.OpenOutputStream(uri);
    		// This img variable is a byte array from an image.
    		output.Write(img, 0, img.Length);
    		output.Flush();
    		output.Close();
    	}
    	catch (System.Exception ex)
    	{
    		Console.Write(ex.ToString());
    	}
    	contentValues.Put(MediaStore.IMediaColumns.IsPending, 1);
    }
    

    For non-media files, Android provides the Storage Access Framework to access them. Please refer to the following documentation:

    In Xamarin, you could use Storage Access Framework directly through FilePicker.

    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 additional answers

Sort by: Most helpful