Share via

Xamarin Forms - Get file path

Yusuf 791 Reputation points
2021-05-03T13:21:25.567+00:00

Hi
I have image image01.jpg in my_folder folder in the android picture folder
How can I get the file path because I want to share the image

Developer technologies | .NET | Xamarin
0 comments No comments

Answer accepted by question author

JessieZhang-MSFT 7,721 Reputation points Microsoft External Staff
2021-05-04T04:14:56.297+00:00

Hello,

Welcome to our Microsoft Q&A platform!

For this, you can use Xam.Plugin.Media to achieve this, which is a simple cross platform plugin to take photos and video or pick them from a gallery from shared code.

And this library has a lot of legacy code that is extremely hard to maintain and update to support the latest OSes without a major re-write.

You can get image path as follows:

 pickPhoto.Clicked += async (sender, args) =>  
      {  
        if (!CrossMedia.Current.IsPickPhotoSupported)  
        {  
          DisplayAlert("Photos Not Supported", ":( Permission not granted to photos.", "OK");  
          return;  
        }  
         var file = await Plugin.Media.CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions  
                      {  
                          PhotoSize =  Plugin.Media.Abstractions.PhotoSize.Medium,  
                      
                      });  
    
        if (file == null)  
          return;    
  
          System.Diagnostics.Debug.WriteLine("path1 = " + file.AlbumPath);  
          System.Diagnostics.Debug.WriteLine("path2 = " + file.Path);  
  
          image.Source = ImageSource.FromStream(() =>  
        {  
          var stream = file.GetStream();  
          file.Dispose();  
          return stream;  
        });  
      };  

For more details, you can check: https://github.com/jamesmontemagno/MediaPlugin .

And there is a sample included in above link, you can try yourself.

Best Regards,

Jessie 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.

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.