0

I'm loading a book as image in my android app

when user presses the next page key, application downloads a new image (in asynctask) and set my new image in my imageview

but after 8 pages ... :| (and I'm not saving previous pages in a variable)

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

Update :

code :

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;
}
7
  • That is obvious. try reducing image size. Commented Feb 8, 2014 at 14:46
  • what ?! but why. it's loading just one image ! :| Commented Feb 8, 2014 at 14:48
  • You should post your code so we can help. It's likely you are not using the BitmapOptions object to specify things like size of the download. Commented Feb 8, 2014 at 14:51
  • updated now having the download method :D Commented Feb 8, 2014 at 14:53
  • You should start with the beginning, for example the training section on the official developer site. You'd have found this : developer.android.com/training/displaying-bitmaps/… A google search would also be a good idea, you'd have found a lot of similar questions ... Commented Feb 8, 2014 at 14:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.