我有一堆图像存储在我的服务器数据库中,作为我想在我的 Android 应用程序中使用的字节数组。
字节数组是从 Windows Phone 7 应用程序中的图像创建的,因此使用 .NET 的 WriteableBitmap (wbmp) 保存到 IsolatedStorageFileStream (isfs):
wbmp.SaveJpeg(isfs, newWidth, newHeight, 0, 90);
在 Android 应用程序中,我有一个 ImageView 小部件,我试图用它来显示其中一张图像。我尝试使用 BitmapFactory 来解码字节数组(有效负载):
Bitmap bmp = BitmapFactory.decodeByteArray(payload, 0, payload.length);
img1.setImageBitmap(bmp);
但这不起作用——当我单步调试调试器时,没有图像显示并且 bmp 为空。这似乎表明 BitmapFactory 无法正确解码字节数组。
对于 Windows Phone 7,我只需将 byte[](现实)加载到 MemoryStream(mstream)中,然后使用该 MemoryStream 调用 Bitmap(bmp)的 SetSource 方法:
mstream = new MemoryStream(reality);
bmp.SetSource(mstream);
所以然后在Android上,我尝试将字节数组读入MemoryFile,然后使用BitmapFactory加载MemoryFile的InputStream:
MemoryFile mf;
try {
mf = new MemoryFile("myFile", payload.length);
mf.writeBytes(payload, 0, 0, payload.length);
InputStream is = mf.getInputStream();
Bitmap bmp = BitmapFactory.decodeStream(is);
img1.setImageBitmap(bmp);
} catch (IOException e) {
e.printStackTrace();
}
但这仍然行不通。
如何在 Android 中成功加载这种格式的字节数组来显示图像?