将Android中的位图转换为缓冲输入流

3

新手问题;

我在内存中有一个位图;

Private Bitmap MyPicture;

然后,稍后我会用照相机从imager中填充我的图片。 我需要使用来自apache commons的FTP客户端上传该照片。

fcon.storeFile("filename", new BufferedInputStream(MyPicture.????));

但是Apache需要一个BufferedInputStream。我该如何将内存中的Bitmap转换为内存流?

谢谢大家!

2个回答

14

这就是我在寻找的东西;

ByteArrayOutputStream stream = new ByteArrayOutputStream();
si.Image.compress(CompressFormat.JPEG, 100, stream);
InputStream is = new ByteArrayInputStream(stream.toByteArray());

最后一行是缺失的链接...


嘿@Lectare,你能否包含完整的代码片段?我不知道“si”是什么,以及你的原始片段如何与这个片段配合。 - Sky Kelsey
@SkyKelsey,si.Image 是位图类型。 - dimetil
谢谢伙计!你帮我省了很多时间!;) - chemitaxis
你为什么需要“压缩”它?你将其压缩为“JPEG”,但如果图像是gif格式呢?如果图像是PNG格式,是否会失去所有alpha值?那实际上会发生什么? - John Sardinha

0

将位图转换为字节数组就像这样简单:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
MyPicture.compress(Bitmap.CompressFormat.PNG, 100, baos);
baos.toByteArray();

然后,您可以创建一个BufferedInputStream将字节写入文件。

位图类型是否总是未压缩的? - Dennis
无法使其工作;BufferedInputStream期望一个InputStream,而baos不是... - Dennis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接