xamarin表单应用程序文件未保存在设备存储上

时间:2018-06-14 06:29:44

标签: android xamarin xamarin.forms

我为signpad创建了xamarin表单应用程序,该应用程序捕获符号并将png文件保存到设备存储但文件未写入存储

这是我将代码转换为bytes []

的代码
 var image = await signature.GetImageStreamAsync(SignaturePad.Forms.SignatureImageFormat.Png);//getting png file from here
        var signatureMemoryStream = image as MemoryStream;
        byte[] data = signatureMemoryStream.ToArray();// convert png to bytes[]
        string fileName = "img.png";
        DependencyService.Get<IFileReadWrite>().WriteData(fileName, data);

我创建了用于保存文件的DependencyService(接口)

   public interface IFileReadWrite
   {
          void WriteData(string fileName, byte[] data);
   }

这是我使用native(app.android)api

保存文件的代码
public class FileHelper : IFileReadWrite
{
    public void WriteData(string filename, byte[] data)
    {
        var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        var filePath = Path.Combine(documentsPath, filename);
        File.WriteAllBytes(filePath, data); // this execute without error but file is not saving on path

    }
}

我已经在mainfest中获得了WRITE_EXTERNAL_STORAGE的许可

1 个答案:

答案 0 :(得分:2)

在代码中替换它并尝试:

var documentsPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);

希望这可以解决您的问题。