安卓内存不足错误?
我在此处检索图像时不断收到这些错误。
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
当我尝试使用此代码检索图像时,会发生这种情况。
公共无效 getImages() 抛出 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 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");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想说这是因为您正在使用 BufferedHttpEntity 它尝试将整个响应加载到内存中。你试过没有它吗?
然后处理它?
I would say it's because you're using BufferedHttpEntity it tries to load whole response to memory. Did you tried without it?
and then process it?
我不知道您是否打算这样做,但是您将读入的文本文件中的每一行 URL(?) 附加到 StringBuffer 中,然后将到目前为止读取的 url 分配给 imageUrl。这意味着您基本上正在这样做:
也许这导致了内存问题。
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:
Maybe that is causing the memory issue.