android - 写入下载文件夹给出错误打开失败 EPERM 操作不允许
问题描述
我正在编写一个 Xamarin.Android 应用程序,但本机 Android 的答案就足够了。我正在尝试将 pdf 文件(我下载为字节 [])保存到 Downloads 文件夹。这是我的代码:
File path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
var file = new File(path, "myfile.pdf");
using (var output = new FileOutputStream(file))
{
try
{
await output.WriteAsync(theByteArray);
}
catch (Exception ex)
{
}
}
我得到错误:
/storage/emulated/0/Download/myfile.pdf:打开失败:EPERM(不允许操作)
我在 AndroidManifest.xml 中检查了 WriteExternalStorage
解决方案
从 Android 6.0(API 级别 23)开始,Android 设备需要运行时权限。
如果您需要存储权限,可以使用下面的代码请求。
public class Activity1 : Activity
{
public string TAG
{
get
{
return "Activity1";
}
}
static readonly int REQUEST_STORAGES = 1;
static string[] PERMISSIONS_STORAGES = {
Manifest.Permission.ReadExternalStorage,
Manifest.Permission.WriteExternalStorage
};
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.layout2);
var request_permission = FindViewById<Button>(Resource.Id.btn_request_permission);
var save = FindViewById<Button>(Resource.Id.btn_save);
layout = FindViewById(Resource.Id.layout2);
request_permission.Click += delegate
{
Log.Info(TAG, "Show Storage button pressed. Checking permissions.");
// Verify that all required contact permissions have been granted.
if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) != (int)Permission.Granted
|| ActivityCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) != (int)Permission.Granted)
{
// Contacts permissions have not been granted.
Log.Info(TAG, "Storage permissions has NOT been granted. Requesting permissions.");
RequestContactsPermissions();
}
else
{
// Contact permissions have been granted. Show the contacts fragment.
Log.Info(TAG, "Storage permissions have already been granted.");
}
};
save.Click += async delegate
{
if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) == (int)Permission.Granted
|| ActivityCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) == (int)Permission.Granted)
{
var path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
var file = new File(path, "myfile.pdf");
var theByteArray = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
using (var output = new FileOutputStream(file))
{
try
{
await output.WriteAsync(theByteArray);
}
catch (Exception ex)
{
}
}
}
};
}
/**
* Root of the layout of this Activity.
*/
View layout;
void RequestContactsPermissions()
{
if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.ReadContacts)
|| ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.WriteContacts))
{
// Provide an additional rationale to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
// For example, if the request has been denied previously.
Log.Info(TAG, "Displaying contacts permission rationale to provide additional context.");
// Display a SnackBar with an explanation and a button to trigger the request.
Snackbar.Make(layout, "Storage Permission is needed",
Snackbar.LengthIndefinite).SetAction("OK", new Action<View>(delegate (View obj)
{
ActivityCompat.RequestPermissions(this, PERMISSIONS_STORAGES, REQUEST_STORAGES);
})).Show();
}
else
{
// permissions have not been granted yet. Request them directly.
ActivityCompat.RequestPermissions(this, PERMISSIONS_STORAGES, REQUEST_STORAGES);
}
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
if (requestCode == REQUEST_STORAGES)
{
// Received permission result for permission.
Log.Info(TAG, "Received response for Storage permission request.");
// Check if the only required permission has been granted
if ((grantResults.Length == 1) && (grantResults[0] == Permission.Granted))
{
Log.Info(TAG, "Storage permission has now been granted.");
}
else
{
Log.Info(TAG, "Storage permission was NOT granted.");
}
}
else
{
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
推荐阅读
- ios - 调用断开功能后,Socket io不连接。即连接->断开->连接将不起作用iOS swift
- optimization - 用于列生成的 Google OR 示例
- java - 或 Java For 循环中的条件
- django - 安装 django-heroku 时出错错误:找不到 pg_config 可执行文件
- python - 熊猫适用于未知列
- c# - 为什么 C# Thread.Join 在第一次等待时返回
- python - 有没有一种方法可以检查 img 标签的 src 是否包含用于使用 BS4 进行抓取的特定字符串
- reactjs - 如何在反应中使用pdf js express从pdf中删除突出显示注释
- javascript - 在使用 for 循环创建它们之后,如何在 JavaScript 中定位特定的 div?
- sql - 如何从 oracle SQL 的结果中选择唯一记录?