Android:应用程序在加载许多图像时崩溃

时间:2014-02-08 14:45:26

标签: java android android-asynctask

我正在我的Android应用程序中加载一本书作为图像

当用户按下一页键时,应用程序下载一个新图像(在asynctask中)并在我的imageview中设置我的新图像

但经过8页...:| (我不会将以前的页面保存在变量中)

02-08 18:03:17.340: E/AndroidRuntime(18781): FATAL EXCEPTION: AsyncTask #3
02-08 18:03:17.340: E/AndroidRuntime(18781): java.lang.RuntimeException: An error occured while executing doInBackground()
02-08 18:03:17.340: E/AndroidRuntime(18781):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
02-08 18:03:17.340: E/AndroidRuntime(18781):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
02-08 18:03:17.340: E/AndroidRuntime(18781):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
02-08 18:03:17.340: E/AndroidRuntime(18781):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
02-08 18:03:17.340: E/AndroidRuntime(18781):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-08 18:03:17.340: E/AndroidRuntime(18781):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-08 18:03:17.340: E/AndroidRuntime(18781):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-08 18:03:17.340: E/AndroidRuntime(18781):    at java.lang.Thread.run(Thread.java:856)
02-08 18:03:17.340: E/AndroidRuntime(18781): Caused by: java.lang.OutOfMemoryError
02-08 18:03:17.340: E/AndroidRuntime(18781):    at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
02-08 18:03:17.340: E/AndroidRuntime(18781):    at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:428)
02-08 18:03:17.340: E/AndroidRuntime(18781):    at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:446)
02-08 18:03:17.340: E/AndroidRuntime(18781):    at com.abc.cde.ShowChapterActivity$LoadChapterPages.doInBackground(ShowChapterActivity.java:233)
02-08 18:03:17.340: E/AndroidRuntime(18781):    at com.abc.cde.ShowChapterActivity$LoadChapterPages.doInBackground(ShowChapterActivity.java:1)
02-08 18:03:17.340: E/AndroidRuntime(18781):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-08 18:03:17.340: E/AndroidRuntime(18781):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-08 18:03:17.340: E/AndroidRuntime(18781):    ... 4 more

更新:

代码:

public byte[] getUrlImgContent(String urlstring) throws IOException
{
    byte[] imageRaw = null;
    URL url = new URL(urlstring);

    HttpURLConnection urlConnection = (HttpURLConnection) url
            .openConnection();
    urlConnection.setUseCaches(false);
    urlConnection.connect();
    if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK)
    {
        try
        {
            InputStream in = new BufferedInputStream(
                    urlConnection.getInputStream());
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            int c;
            while ((c = in.read()) != -1)
            {
                out.write(c);
            }
            out.flush();

            imageRaw = out.toByteArray();

            urlConnection.disconnect();
            in.close();
            out.close();
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return imageRaw;
    }
    return null;
}

0 个答案:

没有答案