【问题标题】:Java.IO.FileNotFoundException: open failed: ENOENT (No such file or directory)'Java.IO.FileNotFoundException:打开失败:ENOENT(没有这样的文件或目录)'
【发布时间】:2022-01-02 03:11:47
【问题描述】:

得到那个错误:

Java.IO.FileNotFoundException: '/storage/emulated/0/meusarquivos/dd.pdf: open failed: ENOENT (No such file or directory)'

在这一行(SaveAndView 方法):

var outs = new FileOutputStream(file);

SaveAndView:

public void SaveAndView(string fileName, string contentType, MemoryStream stream)
        {
            try
            {
                string root = null;
                //Get the root path in android device.
                root = Android.OS.Environment.IsExternalStorageEmulated ? Android.OS.Environment.ExternalStorageDirectory.ToString() : System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);

                //Create directory and file 
                var myDir = new Java.IO.File(root + "/meusarquivos");
                myDir.Mkdir();

                var file = new Java.IO.File(myDir, fileName);

                //Remove if the file exists
                if (file.Exists()) file.Delete();

                //Write the stream into the file
                var outs = new FileOutputStream(file);
                outs.WriteAsync(stream.ToArray());

                outs.Flush();
                outs.Close();
            }
            catch (Exception ex)
            {
                throw;
                //PostLog.AppCenterLogExcecao(ex, new Dictionary<string, string> { { "origem", "OrderViewModel - 159" } });
            }
        }

我的 AndroidManifest.xml 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.mobileappxamarinbarcodescanner">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" />
    <application android:label="MobileAppXamarinBarcodeScanner.Android" android:theme="@style/MainTheme"></application>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

我错过了什么?

【问题讨论】:

  • 这可能与您删除导致错误的行上方的行中的文件有关........您需要在执行任何操作之前再次创建文件它。您是说“fileName”而不是“file”吗?
  • @squidwardsface 你的意思是这可能是我删除文件的事实,这可能是问题?如果我只是远程删除该删除行,每次尝试都会附加文件吗?
  • @squidwardsface 我删除了检查和删除文件的行但是我得到了同样的错误
  • 我建议使用 System.IO 类而不是 Java 类来执行此操作
  • @Jason 你可以发布示例作为答案吗?

标签: xamarin.forms xamarin.android


【解决方案1】:

是的,您可以使用System.IO 代替Java 类来实现此目的。

我创建了一个简单的演示来实现这一点,它可以工作。

可以参考以下代码:

  using System.IO;

  public void SaveAndView(string fileName)
  {
      try
      {

          string text = "hello world";
          byte[] data = Encoding.ASCII.GetBytes(text);

          string rootPath = Android.OS.Environment.IsExternalStorageEmulated ? Android.OS.Environment.ExternalStorageDirectory.ToString() : System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
          var filePathDir = Path.Combine(rootPath, "meusarquivos");
          if (!System.IO.File.Exists(filePathDir))
          {
              Directory.CreateDirectory(filePathDir);
          }
          string filePath = Path.Combine(filePathDir, fileName);

          System.Diagnostics.Debug.WriteLine("filePath  is:" + filePath);
          System.IO.File.WriteAllBytes(filePath, data);


      }
      catch (Exception ex)
      {

         System.Diagnostics.Debug.WriteLine(ex.Message);
      }
  }

用法:

 SaveAndView("dd.pdf");

您可以在这里参考官方示例:LocalFiles

更多详情,您可以查看:

https://docs.microsoft.com/en-us/xamarin/android/platform/files/external-storage?tabs=windows.

【讨论】:

  • @Arie - 注意这个答案如何有逻辑来创建目录,如果它不存在。
猜你喜欢
  • 1970-01-01
  • 2022-08-23
  • 1970-01-01
  • 1970-01-01
  • 2013-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多