Android的岗位高分辨率图像运行内存 [英] Android post high res image running out of memory
问题描述
好日子的开发伙伴。
我正忙着为Android从一个应用程序上传图片。结果
我也得到了它的工作(code将在下面跟随)。结果
但是,当我送大图像(10百万像素),我的应用程序崩溃与内存外的异常。结果
针对此解决方案是使用COM pression但如果我要发送的全尺寸图像?结果
我想,或许是甲流,但我不familair与流。也许URLConnection的可能帮助,但我真的不知道。结果
我给的文件名文件的名称[0至9999] .JPG
与图像日期的岗位价值被称为Filedata上
我给了岗位价值dropboxid一个UID
在code以下的工作,但我很想解决我的问题是prevents我发送高清图片。
亲切的问候
尝试
{
ByteArrayOutputStream BOS =新ByteArrayOutputStream();
bitmap.com preSS(比较pressFormat.JPEG,100,BOS);
字节[]数据= bos.toByteArray(); HttpPost postRequest =新HttpPost(URL_SEND); ByteArrayBody BAB =新ByteArrayBody(数据,文件+垫(random.nextInt(9999)+ 1)+.JPG);
MultipartEntity reqEntity =新multipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart(Filedata上,BAB);
reqEntity.addPart(dropboxId,新StringBody(URLEn coder.en code(UID)));
postRequest.setEntity(reqEntity); HTT presponse响应= httpClient.execute(postRequest);
的BufferedReader读者=新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent(),UTF-8)。);
串sResponse;
StringBuilder的S =新的StringBuilder(); 而((sResponse = reader.readLine())!= NULL)
{
S = s.append(sResponse);
} 如果(D)Log.i(E发送回应:\\ n+ S);
}
赶上(例外五)
{
如果(D)Log.e(E,错误而发:+ e.getMessage());
返回错误;
}
在使用 ByteArrayOutputStream
,然后调用 #toByteArray()
您有效的一倍的内存量的JPEG使用。 ByteArrayOutputStream
保持与EN codeD JPEG和内部阵列,当你调用 #toByteArray()
它分配一个新阵列和放大器;从内部缓冲区拷贝数据。
考虑编码大的位图到一个临时文件和放大器;使用的FileOutputStream
和的FileInputStream
为en code和发送图像。
如果没有上传 - 您的应用程序在内存中的只是巨大的位图我假设生存很好
编辑: FileBody
文件IMG =新的文件(这是你把你的图片的路径)
ContentBody CB =新FileBody(IMG,文件+垫(random.nextInt(9999)+ 1)+.JPG,图像/ JPG,NULL);
MultipartEntity reqEntity =新multipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart(Filedata上,CB);
reqEntity.addPart(dropboxId,新StringBody(URLEn coder.en code(UID)));
Good day fellow developers.
I'm busy for android to upload images from a app.
I also got it working (code will follow below).
But when i send large images (10 megapixels) my app crashes with an out-of-memory exception.
A solution for this is to use compression but what if i want to send the full size image?
I think perhaps something with a stream but i'm not familair with streams. Perhaps urlconnection might help to but i really have no idea.
I give the filename the name File[0 to 9999].jpg The post value with the image date is called Filedata I give a UID for the post value dropboxid
The code below works but i would love to solve my problem that prevents me from sending high res images.
kind regards
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;
}
When using ByteArrayOutputStream
and then calling #toByteArray()
you've effectively doubled the amount of memory the JPEG is using. ByteArrayOutputStream
keeps an internal array with the encoded JPEG and when you call #toByteArray()
it allocates a new array & copies the data from the internal buffer.
Consider encoding large bitmaps to a temporary file & using FileOutputStream
and FileInputStream
to encode and send the image.
Without "uploading" - your app survives "nicely" with the just the huge bitmap in memory I assume?
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)));
这篇关于Android的岗位高分辨率图像运行内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!