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

时间:2022-12-12 23:00:24

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

我正在尝试将一个图像从byte[]转换为位图,以显示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[]的值由数据库获取,我检查它是否为空。之后,我想要转换形象,但不能成功。程序显示位图的值为空。

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) {

        }

2 个解决方案

#1


6  

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

从您的代码中,似乎您获取了字节数组的一部分,并在该部分中使用了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 getByte use the getBlob method of the ResultSet class. Let's say that column name is image_data. Having this info, change your code to something like this:

您需要更改您的select查询(或者至少知道该列的名称(或索引),该列的blob数据存储在您的db中)。getByte的intead使用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) {

    }

#2


13  

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.

您需要在循环之外放上面一行,因为它接受字节数组并转换为位图。

P.S. :- here imageData is bytes array of Image

这里的imageData是图像的字节数组

#1


6  

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

从您的代码中,似乎您获取了字节数组的一部分,并在该部分中使用了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 getByte use the getBlob method of the ResultSet class. Let's say that column name is image_data. Having this info, change your code to something like this:

您需要更改您的select查询(或者至少知道该列的名称(或索引),该列的blob数据存储在您的db中)。getByte的intead使用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) {

    }

#2


13  

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.

您需要在循环之外放上面一行,因为它接受字节数组并转换为位图。

P.S. :- here imageData is bytes array of Image

这里的imageData是图像的字节数组