问题
How do I access public Documents folder on an Android phone's internal storage? I need to read and write publicly-accessible files into the Documents folder.
回答1:
don't forget to add permission to androidmanifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
sample of writing
// write on SD card file data in the text box
try {
File myFile = new File("/sdcard/mysdfile.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
myOutWriter.append(txtData.getText());
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
"Done writing SD 'mysdfile.txt'",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
sample of reading
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();
}
回答2:
Well I have solved this myself :)
It's pretty much the same as you would do it for a normal Windows Desktop application:
Create and read file from Documents folder (on an Android device):
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;
}
}
}
Complete Sample:
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;
}
}
}
};
}
}
}
}
回答3:
With permissions to read external storage, this code should work for you.
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();
回答4:
if you are running on sdk higher than 23, android version higher than 6, you should implement access request to user. please find more information here on this link
来源:https://stackoverflow.com/questions/27396592/how-do-i-access-read-from-write-to-documents-folder-in-internal-storage-on-andro