Memory problems when posting files from android
When I try to upload images or larger files from my android app it crashes with an OutOfMemoryException. Im wondering if there are any alternate ways of doing this.
Ive had the application crash in two different spots:
Base64.encodeToString(bytes, Base64.DEFAULT);
And here where the base64 string is one of the values in the nameValuPairs collection.
HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost(uo.WebServiceURL);
UrlEncodedF开发者_如何学CormEntity entity = new UrlEncodedFormEntity(nameValuePairs); // On this line
httppost.setEntity(entity);
HttpResponse response = client.execute(httppost);
Any ideas?
If you do it like that, the whole POST must be buffered in memory. This is because it needs to send the Content-Length header first.
Instead, I think you want to use the Apache HTTP libraries, including http://developer.android.com/reference/org/apache/http/entity/FileEntity.html . That will let it figure out the length before reading the file. You can use this answer as a starting point. But the second parameter to the FileEntity constructor should be a mime type (like image/png, text/html, etc.).
Posting a large file in Android
Check This also .........
As Schnapple says your question seems very broad and is confusing to read and understand.
Here is some general code to send a HTTP POST and get a response from a server though that may be helpful.
public String postPage(String url, File data, boolean returnAddr) {
ret = null;
httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
httpPost = new HttpPost(url);
response = null;
FileEntity tmp = null;
tmp = new FileEntity(data,"UTF-8");
httpPost.setEntity(tmp);
try {
response = httpClient.execute(httpPost,localContext);
} catch (ClientProtocolException e) {
System.out.println("HTTPHelp : ClientProtocolException : "+e);
} catch (IOException e) {
System.out.println("HTTPHelp : IOException : "+e);
}
ret = response.getStatusLine().toString();
return ret;
}
It is because the image size is too high, there is some explanation in to how to resolve this issue, i will point you to a question that has already been asked.
Strange out of memory issue while loading an image to a Bitmap object
hope this helps.
精彩评论