Android发布高分辨率图像内存不足 [英] Android post high res image running out of memory

查看:34
本文介绍了Android发布高分辨率图像内存不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位开发者好.

我正忙着让 Android 从应用上传图片.
我也让它工作了(代码如下).
但是当我发送大图像(10 兆像素)时,我的应用程序因内存不足异常而崩溃.
一个解决方案是使用压缩,但如果我想发送完整尺寸的图像怎么办?
我想也许有流的东西,但我不熟悉流.也许 urlconnection 可能会有所帮助,但我真的不知道.

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.

我将文件名命名为 File[0 to 9999].jpg带有图像日期的 post 值称为 Filedata我为 post 值 dropboxid 提供了一个 UID

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.

亲切的问候

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:
" + s);
}
catch (Exception e)
{
    if(d) Log.e(E, "Error while sending: " + e.getMessage());
    return ERROR;
}

推荐答案

当使用 ByteArrayOutputStream 然后调用 #toByteArray() 你已经有效地加倍 JPEG 正在使用的内存量.ByteArrayOutputStream 使用编码的 JPEG 保存一个内部数组,当您调用 #toByteArray() 时,它会分配一个 new 数组 &从内部缓冲区复制数据.

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.

考虑将大型位图编码为临时文件 &使用 FileOutputStreamFileInputStream 对图像进行编码和发送.

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?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆