You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Studio自动生成Android Manifest时,Android 10(API 29)下如何手动添加存储写入权限?

Fixing Internal Storage Write Permissions on Android 10 (API 29) in Visual Studio

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

  1. Open your Android project's Properties → Android Manifest.
  2. Scroll to the Application section.
  3. In the Metadata list, click Add.
  4. For Name, enter android.requestLegacyExternalStorage.
  5. For Value, enter true.
  6. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 17:42:51