Android:如何将字节数组转换为位图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11613594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:14:24  来源:igfitidea点击:

Android: how to convert byte array to Bitmap?

androidbitmapbytearray

提问by Benben

I'm trying to convert one image from byte[] to Bitmap to show the image in the Android application.

我正在尝试将一张图像从 byte[] 转换为 Bitmap 以在 Android 应用程序中显示该图像。

byte[]'s value is got by database and I checked that it was not null. After that, I would like to convert the image but could not success. The program shows that Bitmap's value is null.

byte[] 的值是由数据库获取的,我检查了它不为空。之后,我想转换图像但无法成功。程序显示Bitmap 的值为空。

I think there are some problems in converting process.

我认为转换过程中存在一些问题。

If you know any tips, please show me.

如果您知道任何提示,请告诉我。

byte[] image = null;
Bitmap bitmap = null;
        try {
            if (rset4 != null) {
                while (rset4.next()) {
                    image = rset4.getBytes("img");
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    bitmap = BitmapFactory.decodeByteArray(image, 0, image.length, options);
                }
            }
            if (bitmap != null) {
                ImageView researcher_img = (ImageView) findViewById(R.id.researcher_img);
                researcher_img.setImageBitmap(bitmap);
                System.out.println("bitmap is not null");
            } else {
                System.out.println("bitmap is null");
            }

        } catch (SQLException e) {

        }

采纳答案by Angelo

From your code, it seems that you take a portion of the byte array and use the BitmapFactory.decodeByteArraymethod in that portion. You need to supply the whole byte array in the BitmapFactory.decodeByteArraymethod.

从您的代码中,您似乎获取了字节数组的一部分并BitmapFactory.decodeByteArray在该部分使用了该方法。您需要在BitmapFactory.decodeByteArray方法中提供整个字节数组。

EDIT from comments

从评论编辑

You need to change your select query (or at least know the name (or the index) of the column that has the blob data of the image stored in your db). Also intead of getByteuse the getBlobmethod of the ResultSetclass. Let's say that column name is image_data. Having this info, change your code to something like this:

您需要更改选择查询(或至少知道具有存储在数据库中的图像的 blob 数据的列的名称(或索引))。也可以getByte使用ResultSet类的getBlob方法。假设列名是. 有了这个信息,把你的代码改成这样:image_data

byte[] image = null;
Bitmap bitmap = null;
    try {
        if (rset4 != null) {
                Blob blob = rset4.getBlob("image_data"); //This line gets the image's blob data
                image = blob.getBytes(0, blob.length); //Convert blob to bytearray
                BitmapFactory.Options options = new BitmapFactory.Options();
                bitmap = BitmapFactory.decodeByteArray(image, 0, image.length, options); //Convert bytearray to bitmap
        //for performance free the memmory allocated by the bytearray and the blob variable
        blob.free();
        image = null;
        }
        if (bitmap != null) {
            ImageView researcher_img = (ImageView) findViewById(R.id.researcher_img);
            researcher_img.setImageBitmap(bitmap);
            System.out.println("bitmap is not null");
        } else {
            System.out.println("bitmap is null");
        }

    } catch (SQLException e) {

    }

回答by AAnkit

use below line to convert bytes into Bitmap, it is working for me.

使用下面的行将字节转换为位图,它对我有用。

  Bitmap bmp = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);

you need to put above line outside of loop, as it takes Bytes Array and convert into Bitmap.

您需要将上面的行放在 loop 之外,因为它需要 Bytes Array 并转换为 Bitmap。

P.S. :- here imageData is bytes arrayof Image

PS :- 这里imageData 是图像的字节数组