How to get the path to the internal memory of the

2019-08-20 08:09发布

问题:

How to get the path to the internal memory of the smartphone and the DCIM folder, then to save the photo there?

string directory = System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
FileStream file = new FileStream(System.IO.Path.Combine(directory, "newProdict_" + product.Width + "x" + product.Height + ".png"), FileMode.OpenOrCreate);

回答1:

You can get the DCIM folder like that:

var dcimFolder = Android.OS.Environment.GetExternalStoragePublicDirectory(System.Environment.DirectoryDcim).Path;

For writing files you can use the nuget package PCLStorage so you already have everything in a cross platform manner:

var folder = await FileSystem.Current.GetFolderFromPathAsync(dcimFolder);
var file = await folder.CreateFileAsync("image.jpg", CreationCollisionOption.ReplaceExisting);
byte[] buffer = new byte[100];
using (System.IO.Stream stream = await file.OpenAsync(FileAccess.ReadAndWrite))
{
    stream.Write(buffer, 0, 100);
}