Android out of memory error?
I keep getting these errors while retreiving images here.
07-26 17:21:29.194: ERROR/AndroidRuntime(396): java.lang.RuntimeException: An error occured while executing doInBackground()
07-26 17:21:29.194: ERROR/AndroidRuntime(396): at android.os.AsyncTask$3.done(AsyncTask.java:200)
07-26 17:21:29.194: ERROR/AndroidRuntime(396): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
07-26 17:21:29.194: ERROR/AndroidRuntime(396): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
07-26 17:21:29.194: ERROR/AndroidRuntime(396): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
07-26 17:21:29.194: ERROR/AndroidRuntime(396): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
07-26 17:21:29.194: ERROR/AndroidRuntime(396): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
07-26 17:开发者_运维知识库21:29.194: ERROR/AndroidRuntime(396): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
07-26 17:21:29.194: ERROR/AndroidRuntime(396): at java.lang.Thread.run(Thread.java:1019)
07-26 17:21:29.194: ERROR/AndroidRuntime(396): Caused by: java.lang.OutOfMemoryError
It occurs here when i try to retreive the images with this code.
public void getImages() throws IOException{
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httppost = new HttpGet("https://sites.google.com/site/theitrangers/images/webImages.txt");
HttpResponse response;
response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
InputStream is = buf.getContent();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line + "\n");
imageUrl = total.toString();
Log.v("getImage1", "Retreived image");
}
}
I would say it's because you're using BufferedHttpEntity it tries to load whole response to memory. Did you tried without it?
InputStream is = response.getEntity().getContent();
and then process it?
I don't know if you are intending to do this, but you are appending each line of URLs(?) from the text file you read in to the StringBuffer and then you assign the urls read up till now to imageUrl. That means you are basically doing this:
imageUrl = URL1
imageUrl = URL1, URL2
...
imageUrl = URL1, URL2, ... URLn
Maybe that is causing the memory issue.
精彩评论