question

SreejithSree-2948 avatar image
0 Votes"
SreejithSree-2948 asked SreejithSree-2948 commented

Xamrin Forms: How to read the details of a file stored in device's external storage?

I have implemented creating a folder and file in the device's external storage and writing data into that file using this thread.

Now I am trying to get the details of the file. For that, I have added a new function in the interface like below.

 //Interface
 public interface IAccessFile
 {
     void CreateFile(string text);
     Java.IO.File GetFileDetails();
 }
    
 //Android implementation
 public Java.IO.File GetFileDetails()
 {
     string rootPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
     var filePathDir = Path.Combine(rootPath, "StockPDT");
     if (File.Exists(filePathDir))
     {
         string filePath = Path.Combine(filePathDir, "STOCK.TXT");
         Java.IO.File file = new Java.IO.File(filePath);
         return file;
     }
     else
     {
         return null;
     }
 }

But the problem is with the interface function part, getting below error":

The type or namespace name 'Java' could not be found (are you missing a using directive or an assembly reference?)

Screenshot:

79045-capture.png

If I return the file from the android part like above, it is easy to get the file details in the portable project. Instead of File, I try to return the file path, but it is empty. Is there any other way to get the file details other than this?


dotnet-xamarin
capture.png (11.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
2 Votes"
JarvanZhang-MSFT answered SreejithSree-2948 commented

Hello,​

Welcome to our Microsoft Q&A platform!

The type or namespace name 'Java' could not be found (are you missing a using directive or an assembly reference?)

This is because the Java.IO.File api is from natvie Android, we cannot use it in the shared project. And the 'file' doesn't work for the other platforms.

I try to return the file path, but it is empty. Is there any other way to get the file details other than this?

What details do you want to get from the file? Try to access the file using the file path, you could return the path string in the 'CreateFile' method. Then get the info of the file with the path.

Check the code:

//define the inface in the shared project 
public interface IAccessFileService
{
    string CreateFile(string FileName);//change the return type to string to return the path string
    void GetFileDetails(string path);
}

//implement the service on the android platform
[assembly: Xamarin.Forms.Dependency(typeof(AccessFileImplement))]
namespace TestApplication.Droid
{
    public class AccessFileImplement : IAccessFileService
    {
        string IAccessFileService.CreateFile(string FileName)
        {
            string text = "hello world";
            byte[] data = Encoding.ASCII.GetBytes(text);
            string rootPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
            var filePathDir = Path.Combine(rootPath, "folder");
            if (!File.Exists(filePathDir))
            {
                Directory.CreateDirectory(filePathDir);
            }
            string filePath = Path.Combine(filePathDir, FileName);
            Java.IO.File file = new Java.IO.File(filePath);
            Java.IO.FileOutputStream outputStream = new Java.IO.FileOutputStream(file, true);
            outputStream.WriteAsync(data);

            return filePath;
        }
    }

    void IAccessFileService.GetFileDetails(string path)
    {
        //perform the work
    }
}

//consume the function code in the shared project
var path = DependencyService.Get<IAccessFile>().CreateFile("myfile.txt");
DependencyService.Get<IAccessFile>().GetFileDetails(path );


Best Regards,

Jarvan Zhang



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What details do you want to get from the file?

I have already created the file and write some text data into that file, I am trying to read that data.

Try to access the file using the file path, you could return the path string in the 'CreateFile' method. Then get the info of the file with the path.

I tried that, but the path is empty

I tried like below:

 //define the interface in the shared project 
 public interface IAccessFileService
 {
     string GetFileDetails();
 }

 //Android implementation
 public string GetFileDetails()
 {
  string rootPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
  var filePathDir = Path.Combine(rootPath, "StockPDT");
  if (File.Exists(filePathDir))
  {
      string filePath = Path.Combine(filePathDir, "STOCK.TXT");
      return filePath;
  }
  else
  {
      return "";
  }
 }
    
 //Fetching path, but it is empty
 string path = DependencyService.Get<IAccessFile>().GetFileDetails();
0 Votes 0 ·

I tried that, but the path is empty

Hi, SreejithSree-2948. I tested the code on my side, this is because the 'filePath' is not specfied a value. Please add a breakpoint at the string filePath = Path.Combine(filePathDir, "STOCK.TXT"); line, the code will not be called if the directory exists.

if (File.Exists(filePathDir))
{
    string filePath = Path.Combine(filePathDir, "STOCK.TXT");
    return filePath;
}
else
{
    return "";
}

Try the following code:

public string GetFileDetails()
{
    string rootPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
    var filePathDir = Path.Combine(rootPath, "StockPDT");

    if (!File.Exists(filePathDir))
    {
        Directory.CreateDirectory(filePathDir);
    }
    string filePath = Path.Combine(filePathDir, "STOCK.TXT");

    return filePath
}
0 Votes 0 ·

I will check this and update you soon.

0 Votes 0 ·

It worked and using the file path I got the file details like below, Thanks

 string fileDetails = File.ReadAllText(path);
0 Votes 0 ·