Xamarin Cross Platform: file di immagine da IPhotoPickerService non visualizzati

Aug 22 2020

Come creare un selettore di immagini su Xamarin Forms?

Ha funzionato, è riuscito a trasmettere il file alla directory dell'app "file" da qualsiasi cartella del dispositivo.

Ma quando si seleziona un file di immagine dalla cartella Fotocamera, il file di immagine non viene visualizzato nel tag immagine. Vengono visualizzate le immagini di tutte le altre cartelle. Qualche idea?

...

        Stream stream = await DependencyService.Get<IPhotoPickerService>().GetImageStreamAsync();
        if (stream != null)
        {
            string filePath = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Personal), thewordsetid.ToString());
            FileStream fileStream = File.Open(filePathName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
            stream.Position = 0;
            stream.CopyTo(fileStream);
            fileStream.Flush();
            fileStream.Close();
        }



public class PhotoPickerService : IPhotoPickerService
{
    public Task<Stream> GetImageStreamAsync()
    {
        // Define the Intent for getting images
        Intent intent = new Intent();
        intent.SetType("image/*");
        intent.SetAction(Intent.ActionGetContent);

        // Start the picture-picker activity (resumes in MainActivity.cs)
        MainActivity.Instance.StartActivityForResult(
            Intent.CreateChooser(intent, "Select Photo"),
            MainActivity.PickImageId);

        // Save the TaskCompletionSource object as a MainActivity property
        MainActivity.Instance.PickImageTaskCompletionSource = new TaskCompletionSource<Stream>();

        // Return Task object
        return MainActivity.Instance.PickImageTaskCompletionSource.Task;
    }
}

...

Risposte

WendyZang-MSFT Aug 24 2020 at 15:10

Puoi controllare la finestra di output. È causato dall'immagine troppo grande.

Bitmap too large to be uploaded into a texture (3120x4160, max=4096x4096)

Viene caricata la foto che corrisponde alla dimensione massima (4096x4096). Potresti controllare lo screenshot.

JoeBudeen Aug 26 2020 at 17:12

Da questo post ho scoperto come ridimensionarlo

https://stackoverflow.com/a/56040512/8325235

...

        MediaFile photo = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions { 
                PhotoSize = PhotoSize.MaxWidthHeight, MaxWidthHeight = 4096,
                DefaultCamera = CameraDevice.Rear,
                Directory = "files",
                SaveToAlbum = true
        });

        File.Copy(photo.Path, filePathName);

...