I am trying to show a image saved as blob in my DB. I am using ORMLite and Android 1.6. I created a textView who show a html.FromHtml
这是代码:
//Save the image in DB.
...
Bitmap myBitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().toString()+"/images/imagem.png");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
glossary.setImageBytes(byteArray);
词汇表是词汇表的例子。 谁拥有字段图像Bytes :
@DatabaseField(dataType = DataType.BYTE_ARRAY)
private byte[] imageBytes;
在我的活动中, 我将图像Bytes 传送到字符串, 以便使用 html. FromHtml :
//Activity
...
String htmlContent = "<h1> Title </h1>
<img src=""+glossary.getImageBytes.toString()+"" />"
现在在我的适应器中,我想展示 htmlContent 的内容:
//Adapter
...
holder.tvContent.setText(Html.fromHtml(htmlContent, new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
byte[] data;
data = source.getBytes();
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
//Log.i("bitmap", "bitmap height:" + String.valueOf(bitmap.getHeight) );
//THIS LOG RETURN NPE
Drawable d = null;
d = new BitmapDrawable(getResources(),bitmap);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
}, null));
...
我认为问题在于当我将字节修改为字符串和字符串到字节时。 因为 Bitmap Factory. decodeFile () 所创建的位图正在返回 NPE 。
但我不知道如何解决这个问题。
Any suggestions? thx!