Android的:如何转换字节数组为位图? [英] Android: how to convert byte array to Bitmap?

查看:111
本文介绍了Android的:如何转换字节数组为位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个图像从字节[]转换为位图,以显示在Android应用程序中的图像。

字节[]的值由数据库GOT和我确认它不是空。
在那之后,我想将图像转换,但不能成功。该方案显示,位图的值为null。

我觉得有在转换过程中的一些问题。

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

 字节[]图像= NULL;
位图位图= NULL;
        尝试{
            如果(rset4!= NULL){
                而(rset4.next()){
                    图像= rset4.getBytes(IMG);
                    BitmapFactory.Options选项=新BitmapFactory.Options();
                    位= BitmapFactory.de codeByteArray的(图像,0,image.length,期权);
                }
            }
            如果(位图!= NULL){
                ImageView的researcher_img =(ImageView的)findViewById(R.id.researcher_img);
                researcher_img.setImageBitmap(位图);
                的System.out.println(位图不为空);
            }其他{
                的System.out.println(位为空);
            }        }赶上(的SQLException E){        }


解决方案

从code,看来你把字节数组的一部分,并使用 BitmapFactory.de codeByteArray的法的部分。您需要在 BitmapFactory.de codeByteArray的方法来提供整个字节数组。

从意见修改

您需要改变你的选择查询(或至少知道有存储在数据库中的图像的BLOB数据列的名称(或指数))。同时这一翻译了getByte 中使用的 getBlob 方法=htt​​p://developer.android.com /reference/java/sql/ResultSet.html相对=nofollow>的ResultSet 类。比方说,列名称 IMAGE_DATA 。有了这个信息,你的code更改为这样的事情:

 字节[]图像= NULL;
位图位图= NULL;
    尝试{
        如果(rset4!= NULL){
                一滴一滴= rset4.getBlob(IMAGE_DATA); //此行得到图像的blob数据
                图像= blob.getBytes(0,blob.length); //转换的Blob的ByteArray
                BitmapFactory.Options选项=新BitmapFactory.Options();
                位= BitmapFactory.de codeByteArray的(图像,0,image.length,期权); //转换ByteArray的位图
        //业绩释放由的ByteArray和BLOB变量分配的memmory
        blob.free();
        图像= NULL;
        }
        如果(位图!= NULL){
            ImageView的researcher_img =(ImageView的)findViewById(R.id.researcher_img);
            researcher_img.setImageBitmap(位图);
            的System.out.println(位图不为空);
        }其他{
            的System.out.println(位为空);
        }    }赶上(的SQLException E){    }

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

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.

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

        }

解决方案

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.

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:

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

    }

这篇关于Android的:如何转换字节数组为位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆