0

このコードを使用して、ImageView内にビットマップを配置しています]

    private Bitmap getBitmap(ImageView imageView) {
          imageView.setDrawingCacheEnabled(true);

          imageView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
          imageView.layout(0, 0, imageView.getMeasuredWidth(), imageView.getMeasuredHeight()); 

          imageView.buildDrawingCache(true);
          Bitmap b = Bitmap.createBitmap(imageView.getDrawingCache());
              imageView.setDrawingCacheEnabled(false);
                    return b;
                }});

そしてそれをバイト配列の形でデータベースに保存するこのコード

 public byte[] convertToBytes(Bitmap image) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    Log.d("IMAGE BYTE IS ", imageBytes.toString());
    return imageBytes;
}

このコードを使用して画像をデコードして再表示すると、nullが返されます

 public Bitmap convertToImage(byte[] bytes) {

    ByteArrayInputStream imageStream = new ByteArrayInputStream(bytes);
    return BitmapFactory.decodeStream(imageStream);
}

挿入前と取得後にバイト配列をログに記録しましたが、どちらも同じ値を返します。どこがうまくいかなかったのですか?

4

1 に答える 1

0

ストリームのメモリ参照のみをSQLiteに保存していたことがわかりました。ContentValueを使用してバイト配列をsqliteに挿入することでうまくいきました。

于 2011-05-10T09:22:14.490 に答える