Xamarin Forms:在Android设备的内部存储中写入文件 [英] Xamarin Forms: Write a file in Android device's internal storage

查看:933
本文介绍了Xamarin Forms:在Android设备的内部存储中写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xamarin Forms PCL项目开发一个跨平台应用程序.在这种情况下,我需要在Android的内部存储中写入一个文件,以便可以访问它.我已经授予写入外部存储"权限,但是当我尝试写入文件时,它说访问被拒绝".

I am developing a cross-platform app using Xamarin Forms PCL project. In this I need to write a file in Android's internal storage so that I can access it. I have given Write to External Storage permissions but when I try to write the file it says Access is denied.

这是我尝试过的:

Java.IO.File DataDirectoryPath = Android.OS.Environment.DataDirectory;
string dirPath = DataDirectoryPath.AbsolutePath + "/MyFolder";
bool exists = Directory.Exists(dirPath);
string filepath = dirPath + "/test.txt";
if (!exists)
{
    Directory.CreateDirectory(dirPath);
    if (!File.Exists(filepath))
    {
        File.Create(filepath).Dispose();
        using (TextWriter tw = new StreamWriter(filepath))
        {
            tw.WriteLine("The very first line!");
            tw.Close();
        }
    }
}

推荐答案

在这种情况下,我需要在android的内部存储中写入一个文件,以便我可以访问它.我已经授予写入外部存储"权限,但是当我尝试写入文件时,它说访问被拒绝".

In this I need to write a file in android's internal storage so that i can acccess it. I have given Write to External Storage permissions but when I try to write the file it says Access is denied.

首先,权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />用于外部存储,因此不适用于内部存储.

First of all, the permission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> is for external storage, hence it doesn't work for the internal storage.

根据您的代码:

Java.IO.File DataDirectoryPath = Android.OS.Environment.DataDirectory; 

您使用Data Directory进行写入,该目录归系统所有,并且没有写入权限,因此在SO上对此问题进行了多次讨论,例如:

You used Data Directory for your writing, this directory is owned by system and there is no permission for writing to it, there're several discussions about this issue on SO, for example: Data directory has no read/write permission in Android .

因此,如答案所示,您可以使用getFilesDir()在Xamarin中写入内部存储,它是

So, as it suggested in the answer there, you can use getFilesDir() to write in internal storage, in Xamarin, it is Android.Content.Context.FilesDir Property.

更新:

我根据您的代码编写了一些代码,应该相同,例如:

I wrote some code based on your code, it should be the same, for example:

var dirPath = this.FilesDir + "/MyFolder";
var exists = Directory.Exists(dirPath);
var filepath = dirPath + "/test.txt";
if (!exists)
{
    Directory.CreateDirectory(dirPath);
    if (!System.IO.File.Exists(filepath))
    {
        var newfile = new Java.IO.File(dirPath, "test.txt");
        using (FileOutputStream outfile = new FileOutputStream(newfile))
        {
            string line = "The very first line!";
            outfile.Write(System.Text.Encoding.ASCII.GetBytes(line));
            outfile.Close();
        }
    }
}

我还通过DDMS检查了此文件,可以在以下文件夹中看到它:data\data\yourAPP\files\MyFolder\test.txt:

I also checked this file by DDMS, it can be seen under the folder: data\data\yourAPP\files\MyFolder\test.txt:

我只是测试Xamarin.Android项目,没有尝试对Xamarin.Forms使用DependencyService,如果您要这样做,只需将上面代码中的this替换为您的android上下文.

I just test Xamarin.Android project, didn't tried to use DependencyService for Xamarin.Forms, if you're trying to do that, just replace the word this in the code above to your android context.

这篇关于Xamarin Forms:在Android设备的内部存储中写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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