C# 如何在Android设备上访问/读取/写入内部存储中的Documents文件夹?
如何访问安卓手机内部存储器上的公共文档文件夹?我需要将可公开访问的文件读写到Documents文件夹中。我自己解决了这个问题:) 这与您在普通Windows桌面应用程序中所做的几乎相同: 从Documents文件夹(Android设备上)创建和读取文件:C# 如何在Android设备上访问/读取/写入内部存储中的Documents文件夹?,c#,android,xamarin,xamarin.android,xamarin-studio,C#,Android,Xamarin,Xamarin.android,Xamarin Studio,如何访问安卓手机内部存储器上的公共文档文件夹?我需要将可公开访问的文件读写到Documents文件夹中。我自己解决了这个问题:) 这与您在普通Windows桌面应用程序中所做的几乎相同: 从Documents文件夹(Android设备上)创建和读取文件: string content=“Jason规则”; 字符串filename=“file.txt”; var documents=System.Environment.GetFolderPath(System.Environment.Specia
string content=“Jason规则”;
字符串filename=“file.txt”;
var documents=System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
如果(!Directory.Exists(documents))
{
WriteLine(“目录不存在”);
}
其他的
{
WriteLine(“目录存在”);
File.writealText(文档+@“/”+文件名,内容);
如果(!File.Exists(documents+/“+filename))
{
Console.WriteLine(“未找到文档”);
}
其他的
{
字符串newContent=File.ReadAllText(文档+@“/”文件名);
TextView viewer=FindViewById(Resource.Id.textView1);
如果(查看器!=null)
{
viewer.Text=newContent;
}
}
}
完整样本:
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace ToolbarSample
{
[Activity(Label = "ToolbarSample", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.button);
if (button != null)
{
button.Click += delegate
{
button.Enabled = false;
string content = "Jason rules";
string filename = "file.txt";
var documents = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
if (!Directory.Exists(documents))
{
Console.WriteLine("Directory does not exist.");
}
else
{
Console.WriteLine("Directory exists.");
File.WriteAllText(documents + @"/" + filename, content);
if (!File.Exists(documents + @"/" + filename))
{
Console.WriteLine("Document not found.");
}
else
{
string newContent = File.ReadAllText(documents + @"/" + filename);
TextView viewer = FindViewById<TextView>(Resource.Id.textView1);
if (viewer != null)
{
viewer.Text = newContent;
}
}
}
};
}
}
}
}
使用Android.Content;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用Android.OS;
命名空间工具栏示例
{
[活动(Label=“ToolbarSample”,MainLauncher=true,Icon=“@drawable/Icon”)]
公共课活动:活动
{
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
//从布局资源中获取我们的按钮,
//并在其上附加一个事件
Button Button=FindViewById(Resource.Id.Button);
如果(按钮!=null)
{
按钮。单击+=委派
{
按钮。已启用=错误;
string content=“Jason规则”;
字符串filename=“file.txt”;
var documents=System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
如果(!Directory.Exists(documents))
{
WriteLine(“目录不存在”);
}
其他的
{
WriteLine(“目录存在”);
File.writealText(文档+@“/”+文件名,内容);
如果(!File.Exists(documents+/“+filename))
{
Console.WriteLine(“未找到文档”);
}
其他的
{
字符串newContent=File.ReadAllText(文档+@“/”文件名);
TextView viewer=FindViewById(Resource.Id.textView1);
如果(查看器!=null)
{
viewer.Text=newContent;
}
}
}
};
}
}
}
}
别忘了向androidmanifest.xml添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
阅读样本
try {
File myFile = new File("/sdcard/mysdfile.txt");
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = "";
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + "\n";
}
txtData.setText(aBuffer);
myReader.close();
Toast.makeText(getBaseContext(),
"Done reading SD 'mysdfile.txt'",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
如果您运行的sdk高于23,android版本高于6,则应该实现对用户的访问请求。请在链接上找到更多信息,该链接具有读取外部存储的权限,此代码应适用于您
var path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDocuments);
var filename = System.IO.Path.Combine(path.ToString(), strFile);
System.IO.FileStream fs = new FileStream(filename, FileMode.Open);
byData = new byte[fs.Length];
fs.Read(byData, 0, (int)fs.Length);
fs.Close();
要使示例正常工作,只需在其中抛出一个按钮和textView,并将该按钮命名为“button”。这似乎不是公共文档文件夹。System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)返回/data/user/0/{appId}/files/Hi:)感谢您的回答。我想知道,这是将文件写入外部SD卡还是手机内置卡?我这样问是因为不是每个人都有外卡插槽,也不是每个人都有外卡,甚至不是每个人都使用外卡。请批准答案;)那是SD卡专用的吗?我把这个放在哪里?我得到“错误CS0103名称‘Android’在当前上下文中不存在”到Android项目。您无法从共享库访问Android.OS.Environment。通过GetExternalStoragePublicDirectory返回Java.IO.File类型的方法。不是合并的路径。这在Android 11中是不推荐的
try {
File myFile = new File("/sdcard/mysdfile.txt");
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = "";
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + "\n";
}
txtData.setText(aBuffer);
myReader.close();
Toast.makeText(getBaseContext(),
"Done reading SD 'mysdfile.txt'",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
var path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDocuments);
var filename = System.IO.Path.Combine(path.ToString(), strFile);
System.IO.FileStream fs = new FileStream(filename, FileMode.Open);
byData = new byte[fs.Length];
fs.Read(byData, 0, (int)fs.Length);
fs.Close();