Arie
Arie

Reputation: 3573

Java.IO.FileNotFoundException: open failed: ENOENT (No such file or directory)'

Getting that error:

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

on this line (SaveAndView method):

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" } });
            }
        }

My AndroidManifest.xml looks as follows:

<?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>

What am i missing?

Upvotes: 1

Views: 1746

Answers (1)

Jessie Zhang -MSFT
Jessie Zhang -MSFT

Reputation: 13929

Yes, you can use System.IO to achieve this instead of Java classes.

I created a simple demo to achieve this,and it works.

You can refer to the following code:

  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);
      }
  }

Usage:

 SaveAndView("dd.pdf");

And you can refer to the official sample here: LocalFiles.

For more detail, you can check:

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

Upvotes: 1

Related Questions