如何从设备上的资产保存文件?沙马林 [英] How to save file from assets on my device? Xamarin

查看:27
本文介绍了如何从设备上的资产保存文件?沙马林的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大量资产的程序.我想从我的应用程序中将其中一个保存在 Android 设备上的下载文件夹中.我该怎么做?

I have a program with a lot of assets. I want to save one of them from my application in the Downloads folder on the Android device. How can I do this?

推荐答案

实际的文件复制是直接使用 C# 和 Android Java 框架的组合:

The actual file copy is is straight forward using a mix of C# and the Android Java framework:

var assetName = "someasset.jpg";
var outputName = Path.Combine(Android.OS.Environment.DirectoryDownloads, assetName);
using (var assetStream = context.Assets.Open(assetName))
using (var fileStream = new FileStream(outputName, FileMode.CreateNew))
{
    await assetStream.CopyToAsync(fileStream);
}

那你就得处理应用权限:

Then you have to deal with application permissions:

首先,您需要向清单添加权限.您必须具有写入权限,但很可能还需要读取权限以检查文件是否已存在于下载文件夹等中...

First you need to add the permissions to the manifest. You have to have write access, but most likely need read access also to check if the file already exists in Download folder, etc...

您可以通过 Xamarin.Android 应用程序项目选项(构建/Android 应用程序/所需权限)或在清单中手动设置它们:

You can do this via Xamarin.Android application project Options (Build / Android Application / Required Permissions) or manually set them in the manifest:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

但是,对于 Marshmallow 及以上版本,您还需要在运行时询问用户是否允许应用读取/写入设备的外部存储,有关详细信息,请查看 Xamarin 的博文:

But, with Marshmallow and above, you also have ask the user at runtime if the app is allowed to read/write to the device's external storage, review the blog post by Xamarin for details:

快速/肮脏的版本是您必须请求权限,然后等待这些结果以查看用户是否允许/拒绝该请求:

The quick/dirty version is you have to ask for permissions and then you wait for those results to see if the user allowed/denied that request:

请求权限:

const string writePermission = Manifest.Permission.WriteExternalStorage;
const string readPermission = Manifest.Permission.ReadExternalStorage;
string[] PermissionsLocation =
 {
    writePermission, readPermission
 };
if ((ContextCompat.CheckSelfPermission(this, writePermission) == Permission.Granted) && (ContextCompat.CheckSelfPermission(this, readPermission) == Permission.Granted))
{
    // You already have permission, so copy your files...
}
if (ActivityCompat.ShouldShowRequestPermissionRationale(this, writePermission) && (ContextCompat.CheckSelfPermission(this, readPermission) == Permission.Granted))
{
    // Displaying a dialog would make sense, but for an example this works...
    Toast.MakeText(this, "Will need permission to copy files to your Downloads folder", ToastLength.Long).Show();
    return;
}
ActivityCompat.RequestPermissions(this, PermissionsLocation, 999);

处理请求的结果:

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
    switch (requestCode)
    {
        case 999:
            if (grantResults[0] == Permission.Granted)
            {
                // you have permission, you are allowed to read/write to external storage go do it...
            }
            break;
        default:
            break;
    }
}

这篇关于如何从设备上的资产保存文件?沙马林的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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