12

1 つの画像を byte[] から Bitmap に変換して、Android アプリケーションで画像を表示しようとしています。

byte[] の値はデータベースによって取得され、null でないことを確認しました。その後、画像を変換したいのですが、うまくいきませんでした。プログラムは、Bitmap の値が null であることを示しています。

変換プロセスに問題があると思います。

何かコツをご存知の方、教えてください。

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

        }
4

2 に答える 2

19

以下の行を使用して、バイトをビットマップに変換します。私にとってはうまくいきます。

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

バイト配列を取り、ビットマップに変換するため、上記の行を loop の外に置く必要があります。

PS :- ここでimageData はImage のバイト配列です

于 2012-07-23T13:42:56.473 に答える
9

コードから、バイト配列の一部を取得し、BitmapFactory.decodeByteArrayその部分でメソッドを使用しているようです。BitmapFactory.decodeByteArrayメソッドでバイト配列全体を指定する必要があります。

コメントから編集

選択クエリを変更する必要があります (または少なくとも、db に保存されている画像の blob データを持つ列の名前 (またはインデックス) を知っている必要があります)。また、 ResultSetクラスのメソッドをgetByte使用する代わりに。列名が. この情報があれば、コードを次のように変更します。getBlobimage_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) {

    }
于 2012-07-23T13:41:22.083 に答える