简体   繁体   English

尝试在外部存储上创建文件时访问拒绝的路径

[英]Access to the path denied when trying to create a file on external storage

I want my android app to save a text file with the contents from an EditText . 我希望我的Android应用程序保存一个文本文件,其中包含EditText的内容。 When I do as follows, the new files are saved as expected: 当我执行以下操作时,新文件将按预期保存:

string[] filename = new string[50];
EditText et1 = FindViewById<EditText>(Resource.Id.editText1);
EditText et2 = FindViewById<EditText>(Resource.Id.editText2);
string doc = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
filename[0] = Path.Combine(doc, "Text1");
filename[1] = Path.Combine(doc, "Text2");
File.WriteAllText(filename[0], et1.Text);
File.WriteAllText(filename[1], et2.Text);

But if I change the filename as follows: 但是,如果我按如下所示更改文件名:

filename[0] = Path.Combine(doc, 1.ToString());
filename[1] = Path.Combine(doc, 2.ToString());

The app will combine the determined address but it can not write data there and throws the following exception: 该应用将合并确定的地址,但无法在其中写入数据,并引发以下异常:

Error: Access to this address denied

How I can solve this problem? 我该如何解决这个问题?

I tried reproducing the issue using your code but could not. 我尝试使用您的代码重现该问题,但未能成功。 This tells me that the problem doesn't lie within your code. 这告诉我问题不在于您的代码内。

I was although running on an emulator which may just be the key difference. 我虽然在模拟器上运行,但这可能只是关键的区别。

When you're trying to access an external storage , make sure that you have set the following permission in your manifest: 尝试访问外部存储时 ,请确保在清单中设置了以下权限:

WRITE_EXTERNAL_STORAGE (writing)
READ_EXTERNAL_STORAGE (reading)

In any case, you should really consider if you specifically want to place the data on the external SD card. 无论如何,您都应该真正考虑是否要将数据放置在外部SD卡上。 That may cause issues for users whom don't have one installed. 对于没有安装的用户,这可能会引起问题。 Instead, you can do as follows to access the internal storage as written in Xamarin's documentation for best practices in regards to the storing content on the file system: 相反,您可以按照Xamarin文档的说明访问内部存储,以获取有关在文件系统上存储内容的最佳实践:

System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)

You're exception is because you forgot the permissions in your AndroidMnaifest.xml: 您的例外是因为您忘记了AndroidMnaifest.xml中的权限:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="yournamespace">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <application .../>
</manifest>

You can make a method writeText() and call it twice. 您可以使方法writeText()并调用两次。 You can use an other folder with 您可以使用其他文件夹

string path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;

        writeText(et1.Text, "Text1.txt");
        writeText(et2.Text, "Text2.txt");

    void writeText(string text, string name) {
         string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
         string filename = Path.Combine(path, name);

         using(var streamWriter = new StreamWriter(filename, true)) {
          streamWriter.Write(text);
         }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM