安卓内存不足错误?

发布于 2024-11-26 07:49:22 字数 2128 浏览 0 评论 0原文

我在此处检索图像时不断收到这些错误。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情徒 2024-12-03 07:49:22

我想说这是因为您正在使用 BufferedHttpEntity 它尝试将整个响应加载到内存中。你试过没有它吗?

InputStream is = response.getEntity().getContent();

然后处理它?

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?

近箐 2024-12-03 07:49:22

我不知道您是否打算这样做,但是您将读入的文本文件中的每一行 URL(?) 附加到 StringBuffer 中,然后将到目前为止读取的 url 分配给 imageUrl。这意味着您基本上正在这样做:

imageUrl = URL1
imageUrl = URL1, URL2
...
imageUrl = URL1, URL2, ... URLn

也许这导致了内存问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文