Xamarin CrossPlatform-IPhotoPickerServiceの画像ファイルが表示されない
Aug 22 2020
Xamarinフォームで画像ピッカーを作成するにはどうすればよいですか?
これは機能し、任意のデバイスフォルダからアプリディレクトリ「files」にファイルをストリーミングすることができました。
ただし、カメラフォルダから画像ファイルを選択すると、画像ファイルは画像タグに表示されません。他のすべてのフォルダの画像は表示されます。何か案は?
..。
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;
}
}
..。
回答
WendyZang-MSFT Aug 24 2020 at 15:10
出力ウィンドウを確認できます。画像が大きすぎることが原因です。
Bitmap too large to be uploaded into a texture (3120x4160, max=4096x4096)
最大サイズ(4096x4096)に一致する写真が読み込まれます。スクリーンショットを確認できます。

JoeBudeen Aug 26 2020 at 17:12
この投稿から、サイズを変更する方法を見つけました
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);
..。