问题
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.
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.
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.
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;
}
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);
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.
Thanks for any kind of help in Advance
Phil
回答1:
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>
Thanks for your help @LeoZhu-MSFT
来源:https://stackoverflow.com/questions/56209650/xamarin-android-app-doesnt-request-writeexternalstorage-but-code-works-fine