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

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

问题描述

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

这是我尝试过的:

Java.IO.File DataDirectoryPath = Android.OS.Environment.DataDirectory;string dirPath = DataDirectoryPath.Abs​​olutePath + "/MyFolder";bool 存在 = Directory.Exists(dirPath);字符串文件路径 = dirPath + "/test.txt";如果(!存在){Directory.CreateDirectory(dirPath);如果 (!File.Exists(filepath)){File.Create(filepath).Dispose();使用 (TextWriter tw = new StreamWriter(filepath)){tw.WriteLine("第一行!");tw.Close();}}}

解决方案

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

首先,权限 用于外部存储,因此它不适用于内部存储.

基于您的代码:

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

您使用Data Directory进行编写,该目录归系统所有,没有写入权限,SO上有几个关于此问题的讨论,例如:

我只是测试 Xamarin.Android 项目,并没有尝试将 DependencyService 用于 Xamarin.Forms,如果您要这样做,只需替换单词 this> 在上面的代码中添加到您的 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.

Here is what I tried:

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();
        }
    }
}

解决方案

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.

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.

Based on your code:

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

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 .

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.

Update:

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();
        }
    }
}

I also checked this file by DDMS, it can be seen under the folder: datadatayourAPPfilesMyFolder est.txt:

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天全站免登陆