简体   繁体   中英

How do I create a folder in an external storage and create a file in it. Pixel, Xamarin.Android?

!correction this does not work with android version 11 per pixel!

I have three xamarin projects.forms, android and ios. Ios is not at the center of the problem at the moment. On any Pixel devices and under any Android (I tested 13, 12, 11, 10). The device says I don't have access. "Access to the trail..." On any other devices, namely: Samsung Xiaomi, Bq, Huawei. All devices on different Android platforms:!! and everything works!!! What was done for this: The very first is the manifesto

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

Further, when launching the application, the user requests permission to read and write or to the explorer as a whole. This code is located in MainActivity.

ActivityCompat.RequestPermissions(thisActivity, new string[] {
    Manifest.Permission.PostNotifications, 
    Manifest.Permission.WriteExternalStorage,
    Manifest.Permission.RecordAudio,
    Manifest.Permission.ReadExternalStorage 
}, 1);

In the main xamarin project.forms I get the path to the root folder of the repository: and create the path to the folder I want to create.

folderPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "CorpFiles");

The folderPath variable stores the path to the folder I need. Then I check if there is a folder on this path and create it if necessary

if (!System.IO.File.Exists(folderPath))
      {
         Directory.CreateDirectory(folderPath);
      }

This code works great. But not on Pixel.With tears in my eyes, I ask you to help me figure this out. This is the third week I've been solving a simple problem that I can't find anywhere.

You basically can't use external storage any more.

Some more reading.

https://medium.com/swlh/sample-for-android-storage-access-framework-aka-scoped-storage-for-basic-use-cases-3ee4fee404fc

 https://www.zoftino.com/how-to-create-browse-files-option-in-android
 https://mateuszteteruk.pl/how-to-use-android-storage-access-framework-with-example
 https://thedroidlady.com/2020-08-24-android-scoped-storage-demystified

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM