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

Sreejith Sree 1,251 Reputation points
2021-03-18T06:26:56.007+00:00

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?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,951 Reputation points
    2021-03-18T07:48:45.417+00:00

    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.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful