简体   繁体   中英

Xamarin.forms ImageSource.FromFile doesn't work

I'm using Xam.Plugin.Media to take a picture .

 var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions()
        {
            Directory = "attachments",
            Name = fileName,
            CompressionQuality = 35
        });
        cam.Source = ImageSource.FromFile(file.Path);

above code does work !

file path is (file.Path):

/var/mobile/Containers/Data/Application/F3997E36-78EB-41AF-A37F-FC794BAF30EC/Documents/attachments/13c8ac4e57734a36bded2c2694e27495.jpg

but this code does not show picture in an Image control

var q = "/var/mobile/Containers/Data/Application/F3997E36-78EB-41AF-A37F-FC794BAF30EC/Documents/attachments/13c8ac4e57734a36bded2c2694e27495.jpg"  


 cam.Source = ImageSource.FromFile(q);

You need to save photo in gallery. When your user takes a photo it will still store temporary data, but also if needed make a copy to the public gallery

var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
{
    SaveToAlbum = true
});

Get the public album path

var aPpath = file.AlbumPath; 

Get private path

var path = file.Path;

On iOS, this is the way to load file you saved before. This is because the system regenerate UDID every time the app relaunch.

public Stream LoadSampleStream(string filename) {
    try
    {
        var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        var filePath = Path.Combine(documentsPath, "Sample", filename);
        return new FileStream(filePath, FileMode.Open);
    }
    catch(Exception ex) {
        return null;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM