問題描述
各位開發者好.
我正忙著讓 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.
考慮將大型位圖編碼為臨時文件 &使用 FileOutputStream
和 FileInputStream
對圖像進行編碼和發送.
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發布高分辨率圖像內存不足的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!