ByteArrayOutputStream到FileBody [英] ByteArrayOutputStream to a FileBody
问题描述
我有一个URI来要么采取或我想加载和COM preSS为JPEG,75%的质量的画廊选择的图像。我相信我已经实现了与以下code:
I have a Uri to an image that was either taken or selected from the Gallery that I want to load up and compress as a JPEG with 75% quality. I believe I have achieved that with the following code:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Bitmap bm = BitmapFactory.decodeFile(imageUri.getPath());
bm.compress(CompressFormat.JPEG, 60, bos);
不,我已经藏到一个 ByteArrayOutputStream
名为 BOS
我需要把它添加到一个 MultipartEntity
为 HTTP POST
到网站。 我想不通的是如何给ByteArrayOutputStream转换为FileBody。
Not that I have tucked it into a ByteArrayOutputStream
called bos
I need to then add it to a MultipartEntity
in order to HTTP POST
it to a website. What I can't figure out is how to convert the ByteArrayOutputStream to a FileBody.
推荐答案
使用一个<一个href="http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/content/ByteArrayBody.html"><$c$c>ByteArrayBody$c$c>而不是(自了HTTPClient 4.1),尽管它的名字它需要一个文件名,也:
Use a ByteArrayBody
instead (available since HTTPClient 4.1), despite its name it takes a file name, too:
ContentBody mimePart = new ByteArrayBody(bos.toByteArray(), "filename");
如果你被卡住了HTTPClient 4.0,使用<一个href="http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/content/InputStreamBody.html"><$c$c>InputStreamBody$c$c>而不是:
If you are stuck with HTTPClient 4.0, use InputStreamBody
instead:
InputStream in = new ByteArrayInputStream(bos.toByteArray());
ContentBody mimePart = new InputStreamBody(in, "filename")
(这两个类也有采取addtional MIME类型字符串构造)
(Both classes also have constructors that take an addtional MIME type string)
这篇关于ByteArrayOutputStream到FileBody的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!