Visual Studio自动生成Android Manifest时,Android 10(API 29)下如何手动添加存储写入权限?
Got it, let's tackle this exact issue you're facing. Android 10 introduced Scoped Storage, which locks down direct internal storage access by default—so your standard permission requests won't work unless you explicitly opt into the legacy storage model. Since Visual Studio auto-generates your Android Manifest, here's how to get this working step by step:
Step 1: Add the WRITE_EXTERNAL_STORAGE Permission
First, make sure the core permission is declared. You can do this in two ways:
- Via Code Attribute: Add this assembly-level line to your
MainActivity.cs(or any root-level code file):[assembly: UsesPermission(Android.Manifest.Permission.WriteExternalStorage)] - Via Project Properties: Right-click your Android project → Properties → Android Manifest. Under Required Permissions, check the box for
WRITE_EXTERNAL_STORAGE.
Step 2: Enable Legacy Storage Access (The Critical Missing Piece)
Even with the write permission, Android 10 will block access unless you enable legacy storage via a special metadata flag. Here are two reliable ways to add this in Visual Studio:
Option A: Use Project Properties GUI
- Open your Android project's Properties → Android Manifest.
- Scroll to the Application section.
- In the Metadata list, click Add.
- For Name, enter
android.requestLegacyExternalStorage. - For Value, enter
true. - Save your changes—Visual Studio will merge this into the auto-generated manifest.
Option B: Manually Create AndroidManifest.xml
If the GUI method doesn't stick, create a manual manifest file in your project root (VS will merge it with the auto-generated one):
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yourcompany.yourappname"> <!-- Replace with your app's package name --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:requestLegacyExternalStorage="true"> </application> </manifest>
Option C: Xamarin-Specific Application Class Attribute
If you're using Xamarin, add the flag directly to a custom Application class:
using Android.App; using Android.Runtime; [Application(RequestLegacyExternalStorage = true)] public class MyApp : Application { public MyApp(IntPtr handle, JniHandleOwnership transfer) : base(handle, transfer) { } }
Step 3: Request Permissions at Runtime
Dangerous permissions like WRITE_EXTERNAL_STORAGE still require runtime consent. Add this code to your MainActivity where you need storage access:
using Android.Content; using Android.Content.PM; using AndroidX.Core.App; using AndroidX.Core.Content; protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Check if permission is granted if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) != Permission.Granted) { // Request permission ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.WriteExternalStorage }, 100); // Use any request code you prefer } } // Handle permission result public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults) { base.OnRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == 100) { if (grantResults.Length > 0 && grantResults[0] == Permission.Granted) { // Proceed with storage operations } else { // Show a message to the user explaining why permission is needed } } }
Why Your Previous Attempt Failed
Adding just the permission attribute wasn't enough because Android 10 enables Scoped Storage by default. The requestLegacyExternalStorage flag tells the system to revert to the pre-Android 10 storage model, which lets your app use the WRITE_EXTERNAL_STORAGE permission as expected.
内容的提问来源于stack exchange,提问作者PontiacGTX

