简体   繁体   中英

Display PNG Byte Array in ImageView

Afternoon all.

I am trying to display a PNG image in an ImageView on my mobile android application. 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. I have tried saving the file and setting the image using the URI but to no success.

    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 . Then, call setImageBitmap() on the ImageView with that Bitmap .

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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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