Xamarin.Android应用程序不请求"WriteExternalStorage"(但是代码与"ReadExternalStorage"一起正常工作) [英] Xamarin.Android app doesn't request "WriteExternalStorage" (but code works fine with "ReadExternalStorage")

查看:87
本文介绍了Xamarin.Android应用程序不请求"WriteExternalStorage"(但是代码与"ReadExternalStorage"一起正常工作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有一些C#的使用经验,但是我是Android开发的新手,并且想从一个简单的项目应用程序开始,该应用程序可以在外部存储设备("/storage/emulated/0/"[Android.OS.Environment.ExternalStorageDirectory.AbsolutePath])并读取它.

I already have some experiences with C#, but I'm new to Android-Development and wanted to start with a simple project app, that can create a new directory with a file on the external storage ("/storage/emulated/0/" [Android.OS.Environment.ExternalStorageDirectory.AbsolutePath]) and read it.

我在文档中读到,在较新的SDK中,仅提及AndroidManifest.xml中的权限(在本例中为"ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE")还不够.因此,我添加了一种在运行时请求它们的方法.

In the documentation I read that in the newer SDKs it's not enough to mention the permissions (in this case "ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE") in the AndroidManifest.xml. So I added a method to request them at runtime.

我的问题是,如果我请求"Manifest.Permission.WriteExternalStorage",则模拟器[Android 8.1 API 27](以及带USB调试功能的手机[Android 8.0])无法打开权限对话框.如果我使用我的代码请求"Manifest.Permission.ReadExternalStorage"对话框出现,则可以接受和拒绝,并且操作在设置中可见.

My problem is that the emulator [Android 8.1 API 27] (and my phone with USB debugging [Android 8.0]) doesn't open the permission dialogue, if I request "Manifest.Permission.WriteExternalStorage". If I use my code to request "Manifest.Permission.ReadExternalStorage" the dialogue appears, you can accept and decline and the action visible in the settings.

首先,我尝试将自己的代码编写到文档中( https://github.com/xamarin/monodroid-samples/tree/master/LocalFiles ),但仍然是同样的问题.

First, I tried to write my own code to the documentation (https://docs.microsoft.com/de-de/xamarin/android/platform/files/external-storage), which doesn't open the dialogue. I tried to let a NuGet-Package (Karamunting.Android.PermissionManager) manage the request, same problem: It just asked for the read permission, not write. Now I'm using code that I copied from a GitHub project (https://github.com/xamarin/monodroid-samples/tree/master/LocalFiles), but still the same problem.

这是应该请求权限的方法

This is the method that should request the permission

const int RC_WRITE_EXTERNAL_STORAGE_PERMISSION = 1000;
static readonly string[] PERMISSIONS_TO_REQUEST = { Manifest.Permission.WriteExternalStorage };

bool RequestExternalStoragePermissionIfNecessary(int requestCode)
        {
            if (Android.OS.Environment.MediaMounted.Equals(Android.OS.Environment.ExternalStorageState))
            {
                if (CheckSelfPermission(Manifest.Permission.WriteExternalStorage) == Permission.Granted)
                {
                    Log.Debug(TAG, "Neccessary permissions granted");
                    return false;
                }

                Log.Debug(TAG, "Permissions denied --> requesting");
                if (ShouldShowRequestPermissionRationale(Manifest.Permission.WriteExternalStorage))
                {
                    Snackbar.Make(FindViewById(Android.Resource.Id.Content),
                                  Resource.String.write_external_permissions_rationale,
                                  Snackbar.LengthIndefinite)
                            .SetAction(Resource.String.ok, delegate { RequestPermissions(PERMISSIONS_TO_REQUEST, requestCode); });
                }
                else
                {
                    RequestPermissions(PERMISSIONS_TO_REQUEST, requestCode);
                }

                return true;
            }

            Log.Warn(TAG, "External storage is not mounted; cannot request permission");
            return true;
        }

在单击按钮时调用它,但我也尝试在OnCreate()方法中使用它

It is called on a button click , but I also tried to use it in the OnCreate() method

bool useIntern = RequestExternalStoragePermissionIfNecessary(RC_WRITE_EXTERNAL_STORAGE_PERMISSION);

我还提到了AndroidManifest中的权限

I've also mentioned the permissions in the AndroidManifest

<uses-sdk android:minSdkVersion="23" android:targetSdkVersion="27" />
<uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE" />

我希望如果授予了权限,应用程序将在启动/单击按钮时进行检查(它会检查权限并在控制台中写入权限被拒绝->正在请求".)

I expect that the application will check on startup/ button click if the permission is granted (It does check for permission and writes in the console "Permissions denied --> requesting").

但是在此之后,该应用程序应显示对话框是否允许您的设备上的[…]应用程序?-拒绝-允许".如果我请求写入许可,则不会发生这种情况,但如果我请求读取,则会发生这种情况.

But after that the app should show the dialogue "Allow App to […] on your device? - Deny - Allow". This doesn't happen if I request the write permission, but happens if I request read.

感谢Advance的任何帮助

Thanks for any kind of help in Advance

菲尔

推荐答案

似乎问题不是代码,而是配置.
在AndroidManifest.xml中,我将minSdk从23更改为21,并将权限大写:

It seems that the problem was not the code, but the configuration.
In the AndroidManifest.xml I changed the minSdk from 23 to 21 and the capitalization of the permission:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.testapp" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"></application>
</manifest>

感谢您的帮助@ LeoZhu-MSFT

Thanks for your help @LeoZhu-MSFT

这篇关于Xamarin.Android应用程序不请求"WriteExternalStorage"(但是代码与"ReadExternalStorage"一起正常工作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆