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.05k stars 1.73k forks source link

Image cannot display device picture. #6918

Closed zxyao145 closed 2 years ago

zxyao145 commented 2 years ago

Description

Image cannot display device picture which in "/data/user/0/app/cache/xxx.png", it's display as blank.

And my code as follow:

MainPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp1.MainPage">

    <StackLayout>
        <Button Clicked="Button_Clicked" Text="Pick"></Button>
        <Image x:Name="Img" Source="dotnet_bot.png" WidthRequest="300" HeightRequest="300" ></Image>
    </StackLayout>
</ContentPage>

MainPage.xaml.cs

namespace MauiApp1
{
    public partial class MainPage : ContentPage
    {
        int count = 0;

        public MainPage()
        {
            InitializeComponent();
        }

        private async void Button_Clicked(object sender, EventArgs e)
        {
            var photo = await MediaPicker.PickPhotoAsync();

            var newFile = Path.Combine(FileSystem.CacheDirectory, photo.FileName);
            using (var stream = await photo.OpenReadAsync())
            using (var newStream = File.OpenWrite(newFile))
                await stream.CopyToAsync(newStream);

            await DisplayAlert("newFile", newFile, "OK");
            Img.Source = newFile;
        }
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" />
    <application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <queries>
        <intent>
            <action android:name="android.media.action.IMAGE_CAPTURE" />
        </intent>
    </queries>
</manifest>

BTW, is there any way to pick images from gallery?

Steps to Reproduce

  1. Create a File > New .NET MAUI App
  2. Replace MainPage ans AndroidManifest with the code
  3. connect the local device
  4. run

Version with bug

Release Candidate 1

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

API 30

Did you find any workaround?

Affected platforms: Only tried on Android

Relevant log output

I don't know how to get it, maybe the official can give a tutorial, thanks a lot.
Redth commented 2 years ago

What if you load it into a stream as a workaround?

I'm not sure also if you're running into some issues with how you're trying to access the file with permissions on Android.

ghost commented 2 years ago

Hi @zxyao145. 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.

zxyao145 commented 2 years ago

What if you load it into a stream as a workaround?

I'm not sure also if you're running into some issues with how you're trying to access the file with permissions on Android.

I have tried to use the following code, but it still doesn't work. I'm not sure if it's a problem with my use

var stream = await photo.OpenReadAsync();
long len = stream.Length;
await DisplayAlert("len", len +"", "OK");  // I can observe that the length is greater than 0
Img.Source = ImageSource.FromStream(()=> stream);

Since I didn't find the use document of MediaPicker under the Maui document, I referred to the xamarin document, which contains the relevant permission setting instructions: https://docs.microsoft.com/en-us/xamarin/essentials/media-picker

jfversluis commented 2 years ago

Do other pictures load at all? We've had a bug where no image would load at all. If you load an image from a URL does that work? Or does the .NET bot from the template show?

If it doesn't show any images please wait for RC3 which should be here very soon and see if that fixes this issue.

ghost commented 2 years ago

Hi @zxyao145. 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.

zxyao145 commented 2 years ago

Do other pictures load at all? We've had a bug where no image would load at all. If you load an image from a URL does that work? Or does the .NET bot from the template show?

If it doesn't show any images please wait for RC3 which should be here very soon and see if that fixes this issue.

Hi, load an image from a URL does not work too. And it can only display the picture under Resources\Images, including dotnet_bot.png of course.

Waiting for your fixes , thanks a lot!

jfversluis commented 2 years ago

@zxyao145 RC3 has been released just now which should make it all work again! Could you update and let us know? :)

ghost commented 2 years ago

Hi @zxyao145. 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.

zxyao145 commented 2 years ago

@zxyao145 RC3 has been released just now which should make it all work again! Could you update and let us know? :)

Perfect! In RC3, it works as expected. And I think this issue can be closed. Thank you!