dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.24k stars 1.76k forks source link

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

Open jevonsflash opened 2 years ago

jevonsflash commented 2 years ago

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

jfversluis commented 2 years ago

You mention preview 14, did you try RC2 yet? I know there have been some changes in the image area

ghost commented 2 years ago

Hi @jevonsflash. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

VincentBu commented 2 years ago

I'm using 17.3.0 Preview 1.0 and Image.Source = ImageSource.FromFile(imagePath) also doesn't work for me.

jevonsflash commented 2 years ago

You mention preview 14, did you try RC2 yet? I know there have been some changes in the image area

I have update to latest vs 2022 17.2.0 Preview 5.0, and confirm that the problem still exists.

jevonsflash commented 2 years ago

How to check maui version? my maui-check tool can't connect to their server. All I see is the version of Xamarin in About window of visual studio, the Xamarin version is 17.2.0.169

Symbai commented 2 years ago

Its still bugged and it also happens on iOS.

NJCCJohnWatson commented 2 years ago

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

Symbai commented 2 years ago

I think its better way is read by stream and bind to image

Which is also still bugged in Android and iOS #7074 its currently not possible to load any external images. Whether its from file, stream or web.

NJCCJohnWatson commented 2 years ago

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

ghost commented 2 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

homeyf commented 1 year ago

Verified this issue with Visual Studio Enterprise 17.8.0 Preview 2.0. Can repro on Windows platform with sample project. https://github.com/NJCCJohnWatson/BiliAvatarMAUI image