我想从Bitmap中保存一张图片并在whatsapp中分享,但是,当我把它保存在我的手机中并试图打开它时,图像变成了黑色,所以,当我分享它时,我分享了一个黑色的图像。在我测试的其他手机上,我可以打开保存的图片,但在我测试的这款手机上,我做不到。两者都是相同的Android版本。这部手机出现黑色图像,我可以在预览(图库)上看到照片,但是,当我试图打开它时,它变黑了。
这是我保存位图的代码:public SKBitmap SaveImage(float ySize, SKBitmap bmpFormatted, SKBitmap standardBmp, int SKCanvasHeight)
{
float width = (float)screenSize.Width;
bmpFormatted = new SKBitmap((int)width, SKCanvasHeight);
SKRectI rect = new SKRectI(0, 0, (int)width, (int)ySize);
standardBmp.ExtractSubset(bmpFormatted, rect);
SKData data = bmpFormatted.Encode(SKEncodedImageFormat.Png, 100);
byte[] bmpByte = data.ToArray();
string directory = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
string path = Path.Combine(directory, "bmpSaved.png");
File.WriteAllBytes(path, bmpByte);
return bmpFormatted;
}
这是我用来共享保存的位图的代码:
string directory = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
string path = Path.Combine(directory, "bmpSaved.png");
var _context = Android.App.Application.Context;
Intent sendIntent = new Intent(global::Android.Content.Intent.ActionSend);
sendIntent.PutExtra(global::Android.Content.Intent.ExtraText, "Whatsapp");
sendIntent.SetType("image/*");
sendIntent.PutExtra(Intent.ExtraStream, Android.Net.Uri.Parse("file://" + path));
_context.StartActivity(Intent.CreateChooser(sendIntent, "Share"));
return Task.FromResult(0);
有什么不同或另一种方法吗?因为我需要两个手机都能用。什么好主意吗?
所以如果有人有同样的问题,我有,试试这个(对我来说它工作):
using (var image = surface.Snapshot())
using (var data = image.Encode(SKEncodedImageFormat.Png, 100))
using (var stream = File.OpenWrite(Path.Combine(directory, "1.png")))
{
// save the data to a stream
data.SaveTo(stream);
}
Thanks to: https://stackoverflow.com/a/59393550/14700237