在ImageView中显示PNG字节数组

时间:2016-03-21 12:52:01

标签: java android

下午全部。

我正在尝试在移动Android应用程序的ImageView中显示PNG图像。图像以数据库中的字节数组的形式出现。

我是Android开发的新手,所以我不确定正确的方法来解决这个问题。我尝试使用URI保存文件并设置图像,但没有成功。

    File tempFile = File.createTempFile("NewsImage", lastNewsDTO.ImageExt, null);
    FileOutputStream fos = new FileOutputStream(tempFile);
    fos.write(lastNewsDTO.Image)
    ((ImageView)rootView.findViewById(R.id.ivNewsImage)).setImageURI(Uri.fromFile(tempFile));

我是在正确的路线还是有更好的方法?

感谢阅读,感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

如果您说您的byte[]包含PNG,请使用BitmapFactory and its decodeByteArray() method将其转换为Bitmap。然后,call setImageBitmap() on the ImageView使用Bitmap

不要将不需要写入磁盘的内容写入磁盘。

答案 1 :(得分:0)

简单使用:

byte[] data;

public static void setImageViewWithByteArray(ImageView view, byte[] data) {
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        view.setImageBitmap(bitmap);
    }
相关问题