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;
}
  • That is obvious. try [reducing](http://stackoverflow.com/a/14235425/1777090) image size. – MysticMagicϡ Feb 08 '14 at 14:46
  • what ?! but why. it's loading just one image ! :| – Ebrahim Tahernejad Feb 08 '14 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. – Phil Feb 08 '14 at 14:51
  • updated now having the download method :D – Ebrahim Tahernejad Feb 08 '14 at 14:53
  • You should start with the beginning, for example the training section on the official developer site. You'd have found this : http://developer.android.com/training/displaying-bitmaps/load-bitmap.html A google search would also be a good idea, you'd have found a lot of similar questions ... – 2Dee Feb 08 '14 at 14:54
  • your image is of high size, u should use sample size in Bitmap options. put a smaller image to test and it wont crash on 8th page – con_9 Feb 08 '14 at 14:56
  • do some quick fix by http://stackoverflow.com/a/14359170/1012284 – Padma Kumar Feb 08 '14 at 14:57

0 Answers0