安卓发布高分辨率图像内存不足



各位开发人员,大家好。

我正忙于安卓从应用程序上传图像。
我也让它工作(代码将在下面遵循)。
但是当我发送大图像(1000 万像素)时,我的应用程序崩溃并出现内存不足异常。
解决方案是使用压缩,但是如果我想发送全尺寸图像怎么办?
我想也许是有溪流的东西,但我不熟悉溪流。也许网址连接可能会有所帮助,但我真的不知道。

我给文件名命名为 File[0 到 9999].jpg带有图像日期的帖子值称为文件数据我为帖子值保管箱 ID 提供一个 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() 时,您实际上使 JPEG 使用的内存量翻了一番ByteArrayOutputStream保留一个带有编码JPEG的内部数组,当您调用#toByteArray()时,它会分配一个新数组并从内部缓冲区复制数据。

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

没有"上传" - 您的应用程序在我假设的内存中的巨大位图下"很好地"存活下来?

编辑: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)));

最新更新