question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ImageSource is not work when set Image.Source to file in external storage

See original GitHub issue

Description

When I load an Android External Storage Image using the Image.Source binding ImageSrouce. Image is not displayed .

Steps to Reproduce

I want to read this image:“/storage/emulated/0/Documents/albumart9100466824972896645.jpg” and show it, I assign AlbumArt to my binding object as follows
AlbumArt = ImageSource.FromStream(() => { var b = File.ReadAllBytes("/storage/emulated/0/Documents/albumart9100466824972896645.jpg"); return new MemoryStream(b); });

AlbumArt = ImageSource.FromFile("/storage/emulated/0/Documents/albumart9100466824972896645.jpg");

the image has been readed exactly correct: image image

I tried using ImageSource.FromStream, and using ImageSource.FromFile, it didn’t work (no display, Image control is blank)

The only thing it is work is to use Resources/albumart_placeholder. JPG which is set to MauiImage AlbumArt = "albumart_placeholder"; //it is work

Version with bug

Preview 14

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 11

Did you find any workaround?

No response

Relevant log output

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
NJCCJohnWatsoncommented, Jun 9, 2022

Because the MAUI File api still have bugs,and the developer tell us use FilePicker to get external storage file And the file will not can be read directly(could caused by Android control) . it will be save a virual path on /data/0/[your package name] /cache/xxx/xxx/[yourimagename]. In Android devices you use blow code to get image and set it to <Image/> tag on xaml.

1.First we need a async FilePicker

public async Task<string> TakePath()
        {
            if (MediaPicker.Default.IsCaptureSupported)
            {
                FileResult photo = await MediaPicker.Default.PickPhotoAsync();

                if (photo != null)
                {
                    // save the file into local storage
                    return photo.FullPath;
                }
                else
                {
                    return "";
                }
            }
            else
            {
                return "";

            }
        }    

2.Then use a event to mount FilePicker

    private async void SetDownloadPath(object sender, EventArgs e)
    {
        FileIO fs = new FileIO();
        string path = await fs.TakePath();
        CounterLabel.Text = path;
        //get parent folder path
        FileInfo fi = new FileInfo(path);
        var realPath = fi.Directory;
        savingPath = path;
    }

3.Now we get the picture path,use blow function to set to Image


    private void LoadImageLocal(string path, Image img)
    {        
        var byteArray = File.ReadAllBytes(path);
        img.Source = ImageSource.FromStream(() => new MemoryStream(byteArray));
        //both can use, just for test
        img = new Image
        {
            Source = ImageSource.FromFile(path)
        };
    }

4.Load image from web , its more easiler too

    private void LoadImageUrl(string url, Image img)
    {
        var byteArray = new HttpClient().GetByteArrayAsync(url).Result;
        img.Source = ImageSource.FromStream(() => new MemoryStream(byteArray));
    }

Now we fixed Read issue and my problem is can not be Write to External Storage Only can write file in /data/0/[your package name] /file/ It’s so conveniently!🤣

All code above from my practicing project https://github.com/NJCCJohnWatson/BiliAvatarMAUI And about further content of Filepicker on MSDoc: https://docs.microsoft.com/en-us/dotnet/maui/platform-integration/storage/file-picker?tabs=android

1reaction
NJCCJohnWatsoncommented, Jun 9, 2022

是孙燕姿欸! I think its better way is read by stream and bind to image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set Image.Source to file in external storage in Xamarin.Forms
Image.Source takes object of ImageSource type. I tried ImageSource.FromFile , ImageSource.FromStream and even ImageSource.FromUri ...
Read more >
Set Image.Source to file in external storage in Xamarin.Forms
Source takes object of ImageSource type. I tried ImageSource.FromFile , ImageSource.FromStream and even ImageSource.FromUri . The result is always that image is ...
Read more >
Image.Source Property (Windows.UI.Xaml.Controls)
Typically you set this with a BitmapImage object, constructed with the Uniform Resource Identifier (URI) that describes the path to a valid image...
Read more >
Images
Images. Static Image Resources​. React Native provides a unified way of managing images and other media assets in your Android and iOS apps....
Read more >
How to obtain a URI for an image asset in React Native ...
Another way of importing and handling image assets without using require() no third-party plugins required. I'm currently trying to master ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found