【发布时间】:2016-02-08 10:43:47
【问题描述】:
我知道,这是一个老问题,但我在将 byte[] 编码为位图时遇到了问题...
背景:我正在编写一个接收picturebytes via UDP 的Andoid-App,将它们编码为位图并在image view 中显示图片。
由于我的函数不起作用,我取消了UDP-Connection 进行测试,并将所有image-bytes 写入一个巨大的变量中。所以他们都是正确的...
该函数返回“null”。
我正在使用的功能:
public Bitmap ByteArrayToImage(byte[] imageData)
{
var bmpOutput = BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length);
return bmpOutput;
}
我尝试的另一个功能:
public Bitmap ByteArrayToImage2(byte[] imageData)
{
Bitmap bmpReturn;
bmpReturn = (Android.Graphics.Bitmap) Android.Graphics.Bitmap.FromArray<byte>(imageData);
return bmpReturn;
}
我在网上找到的一个函数:
public static Bitmap bytesToUIImage (byte[] bytes)
{
if (bytes == null)
return null;
Bitmap bitmap;
var documentsFolder = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
//Create a folder for the images if not exists
System.IO.Directory.CreateDirectory(System.IO.Path.Combine (documentsFolder, "images"));
string imatge = System.IO.Path.Combine (documents, "images", "image.jpg");
System.IO.File.WriteAllBytes(imatge, bytes.Concat(new Byte[]{(byte)0xD9}).ToArray());
bitmap = BitmapFactory.DecodeFile(imatge);
return bitmap;
}
最不幸的是,最后一个函数效果不佳,但在这里我承认,我对
string imatge = System.IO.Path.Combine (documents, "images", "image.jpg");
我收到一个错误并将其更改为documentsFolder,因为我猜应该(或可能)是正确的......
提前感谢您的帮助
【问题讨论】:
标签: android image bitmap xamarin byte