【发布时间】:2014-04-02 01:01:22
【问题描述】:
我一直在尝试解决我的问题,寻找类似这样的问题 (Drawable to byte[]),但我无法解决。
我正在从 imageview 设置一个可绘制图片(用户也可以更改它),后来我试图获取这张图片并将其转换为字节数组以将其保存在数据库中。
我的代码:
//imageview1.setImageResource(R.drawable.pictureJM);
imageview1.setImageDrawable(getResources().getDrawable(R.drawable.pictureJM));
Drawable d1=imageview1.getDrawable();
Bitmap bitmap =((BitmapDrawable)d1).getBitmap(); <-- (The application stops here)
ByteArrayOutputStream streamJM = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, streamJM);
byte[] imageInByte = streamJM.toByteArray();
你能告诉我有什么问题吗?
【问题讨论】:
-
我已经尝试过您输入的相同代码,它工作正常。您到底遇到了什么错误?
-
将资源放入
ImageView而不是直接将资源解码为Bitmap到底有什么意义?BitmapFactory.decodeResource(context.getResources(), R.drawable.pictureJM) -
(Arshdeep_somal)- 没有错误,它只是停止执行,但是如果我从图库中放一张带有其他功能的图片,我就可以了……我不明白,很难解释。 (sherpya),我应该如何处理这些代码行?谢谢两位,这个问题我很头疼。
标签: android database imageview bytearray drawable