各位开发人员大家好。
我正忙着让 android 从应用程序上传图像。
我也让它工作了(代码如下)。
但是,当我发送大图像(10 兆像素)时,我的应用程序因内存不足异常而崩溃。
解决方案是使用压缩,但如果我想发送全尺寸图像怎么办?
我想也许与流有关,但我对流不熟悉。也许 urlconnection 可能有帮助,但我真的不知道。
我将文件名命名为 File[0 到 9999].jpg
带有图像日期的发布值称为 Filedata
我为 post value dropboxid 提供了一个 UID
下面的代码有效,但我很想解决阻止我发送高分辨率图像的问题。
亲切的问候
try
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
HttpPost postRequest = new HttpPost(URL_SEND);
ByteArrayBody bab = new ByteArrayBody(data, "File" + pad(random.nextInt(9999) + 1) + ".jpg");
MultipartEntity reqEntity = new multipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("Filedata", bab);
reqEntity.addPart("dropboxId", new StringBody(URLEncoder.encode(uid)));
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while((sResponse = reader.readLine()) != null)
{
s = s.append(sResponse);
}
if(d) Log.i(E, "Send response:\n" + s);
}
catch (Exception e)
{
if(d) Log.e(E, "Error while sending: " + e.getMessage());
return ERROR;
}
使用时ByteArrayOutputStream
然后打电话#toByteArray()
你已经有效地doubledJPEG 使用的内存量。ByteArrayOutputStream
保留一个带有编码 JPEG 的内部数组,当您调用时#toByteArray()
它分配一个newarray & 从内部缓冲区复制数据。
考虑将大位图编码到临时文件并使用FileOutputStream
and FileInputStream
编码并发送图像。
如果没有“上传”-您的应用程序可以“很好地”生存,我认为内存中只有巨大的位图?
Edit: FileBody
File img = new File(this is where you put the path of your image)
ContentBody cb = new FileBody(img, "File" + pad(random.nextInt(9999) + 1) + ".jpg", "image/jpg", null);
MultipartEntity reqEntity = new multipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("Filedata", cb);
reqEntity.addPart("dropboxId", new StringBody(URLEncoder.encode(uid)));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)