简体   繁体   English

在ImageView中显示PNG字节数组

[英]Display PNG Byte Array in ImageView

Afternoon all. 下午全部。

I am trying to display a PNG image in an ImageView on my mobile android application. 我正在尝试在移动Android应用程序的ImageView中显示PNG图像。 The image comes in the form of a byte array from a database. 该图像以来自数据库的字节数组的形式出现。

I'm new to android development, so I'm not sure as to the correct way to go about this. 我是android开发的新手,所以我不确定执行此操作的正确方法。 I have tried saving the file and setting the image using the URI but to no success. 我尝试保存文件并使用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));

Am I on the right lines or is there a better method? 我是对的还是有更好的方法?

Thanks for reading, help is appreciated! 感谢您的阅读,不胜感激!

If you are saying that you have a byte[] that contains a PNG, use BitmapFactory and its decodeByteArray() method to convert that into a Bitmap . 如果您说自己有一个包含PNG的byte[] ,请使用BitmapFactory及其decodeByteArray()方法将其转换为Bitmap Then, call setImageBitmap() on the ImageView with that Bitmap . 然后,使用该Bitmap ImageView上调用setImageBitmap()

Do not write things to disk that do not need to be written to disk. 不要将不需要写入磁盘的内容写入磁盘。

Simple Use this : 简单使用这个:

byte[] data;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM