android - 如何将可绘制位读取为 InputStream

标签 android imageview inputstream

有一些 ImageView 对象。我想将该对象的位/原始数据作为 InputStream 读取。如何做到这一点?

最佳答案

首先获取 ImageView 的背景图片作为 Drawable 的对象:

iv.getBackground();

然后将Drawable图像转换成Bitmap使用

BitmapDrawable bitDw = ((BitmapDrawable) d);
Bitmap bitmap = bitDw.getBitmap();

现在使用ByteArrayOutputStreamBitmap转化为Stream并得到bytearray[];然后 将 bytearray 转换为 ByteArrayInputStream

您可以使用以下代码从 ImageView 获取 InputStream

完整源代码

ImageView iv = (ImageView) findViewById(R.id.splashImageView);
Drawable d = iv.getBackground();
BitmapDrawable bitDw = ((BitmapDrawable) d);
Bitmap bitmap = bitDw.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
System.out.println("........length......" + imageInByte);
ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte);

谢谢 迪帕克

关于android - 如何将可绘制位读取为 InputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6341770/

相关文章:

java - 运行 .jar 时的 getResourceAsStream 文件路径

java - 创建了 Adview,现在怎么办?

android - 如何在 ImageView 中缩放图像,使其显示为 "fits"

android - 在 ScrollView 上启用缩放

android - 在android中显示带有镜像的副本图像

android - 在 android 中缩放图像时图像质量差

java - MATLAB中如何从InputStream读取大量数据

java - 从AudioInputStream重写为ByteArrayOutputStream的优化

android - 恢复 Activity 的通知(数据完好无损)

android - fragment android方向丢失 fragment