2

I'm retrieve the String of image name and having to download bitmap from server For getting String of image name fron JSON data and download using this link :

String str_ImgURL_bmp_pattern = "http://xxxx/xxx/MobileService.svc/DownloadFile/FileName/" + imageName; download_PngFile(str_ImgURL_bmp_pattern);

void download_PngFile(String fileUrl) {
        Log.e("In download_PngFile ", " str_imgList_imageaudioPath = " + imageName);
        Bitmap imagenObtenida = null;
        try {
            URL ImgUrl = new URL(fileUrl);
            HttpURLConnection conn = (HttpURLConnection) ImgUrl.openConnection();
            conn.connect();
            imagenObtenida = BitmapFactory.decodeStream(conn.getInputStream());
            //Log.e("imagenObtenida", " = " + imagenObtenida);

            String fotoname = imageName;
            File file = new File(newFolder, fotoname);
            int sizeOfImage = (int) file.length();
            Log.e("sizeOfImage ", " = " + sizeOfImage + "@ " + imageName);
            if (file.exists()) file.delete();
            try {
                FileOutputStream out = new FileOutputStream(file);
                imagenObtenida.compress(Bitmap.CompressFormat.PNG, 100, out);
                out.flush();
                out.close();
               // Log.e("Png = ", "DownLoad complete");

            } catch (Exception e) {

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

But this some images are download and after sometime app crashes and I get the error in the title in logcat.

java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:278)
            at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
     Caused by: java.lang.OutOfMemoryError
            at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:549)
            at com.example.tazeen.classnkk.AllPosts_Page.download_PngFile(AllPosts_Page.java:896)
            at com.example.tazeen.classnkk.AllPosts_Page.getDoenLoaddata(AllPosts_Page.java:820)
            at com.example.tazeen.classnkk.AllPosts_Page$GetgetAllImagePath_List.doInBackground(AllPosts_Page.java:781)
            at com.example.tazeen.classnkk.AllPosts_Page$GetgetAllImagePath_List.doInBackground(AllPosts_Page.java:771)
            at android.os.AsyncTask$2.call(AsyncTask.java:264)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)

The error at this line :imagenObtenida = BitmapFactory.decodeStream(conn.getInputStream());

How to resolve this issues.

daemmie
  • 6,361
  • 3
  • 29
  • 45
androidTag
  • 5,053
  • 7
  • 19
  • 28

1 Answers1

1

So basically the exception is pretty clear. You are out of memory.

There are some possible solutions:

1. Scale down your images:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap imagenObtenida = BitmapFactory.decodeStream(conn.getInputStream(), null, options);

This is required, if even a single image download causes a crash.

2. Don't decode at the same time

So don't use a ThreadPoolExecutor

3. Combinations For example:

-just use 2 parallel Threads

-check the available memory and set a thread number which fits to the current memory

-catch out of memory exceptions and wait till other thread finish

daemmie
  • 6,361
  • 3
  • 29
  • 45