可绘制到字节[] [英] Drawable to byte[]

查看:30
本文介绍了可绘制到字节[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ImageView 中有一张来自网络的图像.它非常小(一个图标),我想将它存储在我的 SQLite 数据库中.我可以从 mImageView.getDrawable() 得到一个 Drawable 但是我不知道接下来要做什么.我不完全理解 Android 中的 Drawable 类.

I have an image from the web in an ImageView. It is very small (a favicon) and I'd like to store it in my SQLite database. I can get a Drawable from mImageView.getDrawable() but then I don't know what to do next. I don't fully understand the Drawable class in Android.

我知道我可以从 Bitmap 获取一个字节数组,例如:

I know I can get a byte array from a Bitmap like:

Bitmap defaultIcon = BitmapFactory.decodeStream(in);

ByteArrayOutputStream stream = new ByteArrayOutputStream();
defaultIcon.compress(Bitmap.CompressFormat.JPEG, 100, stream);

byte[] bitmapdata = stream.toByteArray();

但是如何从 Drawable 获取字节数组?

But how do I get a byte array from a Drawable?

推荐答案

Drawable d; // the drawable (Captain Obvious, to the rescue!!!)
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitmapdata = stream.toByteArray();

这篇关于可绘制到字节[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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