Android post high res image running out of memory

前端 未结 1 1176
春和景丽
春和景丽 2021-01-15 10:48

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 (

1条回答
  •  梦毁少年i
    2021-01-15 11:07

    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)));
    

    0 讨论(0)
提交回复
热议问题