androidTag
androidTag

Reputation: 5213

Android : OutOfMemoryError Error at Bitmap

I'm getting error of java.lang.OutOfMemoryError and crash the app while run the application for download the multiple images from server and stored into SD card.I have some changes in gradle.properties and change the line # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=1024m -XX:MaxHeapSize\=1024m -XX:+HeapDumpOnOutOfMemoryError -Xmx1024m -Dfile.encoding=UTF-8. And trying to call the System.gc(); also .But getting same error again and again.How to solve this issues. Thanks in advanced.

here is my code

The error at this line imagenObtenida = BitmapFactory.decodeStream(conn.getInputStream(), null, options);

Log.e("fileUrl ", " = " + fileUrl + " ImageName = " + ImageName);
            URL ImgUrl = new URL(fileUrl);
            HttpURLConnection conn = (HttpURLConnection) ImgUrl.openConnection();
            conn.connect();
            int lenghtOfImage_File = conn.getContentLength();
            Log.e("lenghtOfImage_File ", " = " + lenghtOfImage_File);


                System.gc();
                if(imagenObtenida != null)
                {
                    imagenObtenida.recycle();
                    imagenObtenida = null;
                }
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inTempStorage = new byte[16*1024];
                options.inPurgeable = true;
                options.inSampleSize = 1;

                imagenObtenida = BitmapFactory.decodeStream(conn.getInputStream(), null, options);
                newFolder = new File(Environment.getExternalStorageDirectory().getPath() + File.separator + "classnkk_images");

                File file = new File(newFolder, ImageName);

Here is my Log error

10-16 11:16:20.978    1135-1152/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    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 com.example.tazeen.classnkk.AllPosts_Page.download_PngFile(AllPosts_Page.java:1128)
            at com.example.tazeen.classnkk.AllPosts_Page.getDoenLoaddata(AllPosts_Page.java:735)
            at com.example.tazeen.classnkk.AllPosts_Page$GetgetDoenLoaddata.doInBackground(AllPosts_Page.java:705)
            at com.example.tazeen.classnkk.AllPosts_Page$GetgetDoenLoaddata.doInBackground(AllPosts_Page.java:701)
            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)

Upvotes: 0

Views: 150

Answers (2)

vishnus
vishnus

Reputation: 728

The image that u r trying to download from the server might be large and hence while trying to decode it, it results in OOM exception.

Check this post on developer's site for handling bitmaps here

Upvotes: 0

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75798

java.lang.OutOfMemoryError: Requested size exceeds VM limit. This error indicates that a Java application attempts ti allocate an array, whose size is larger than the heap size.

The OutOfMemoryError extends the VirtualMachineError class, which indicates that the JVM is broken, or it has run out of resources and cannot operate.

Verify that your application does not store unnecessary information. Store and maintain only those pieces of information required for the proper execution of your Java application.

Strange out of memory issue while loading an image to a Bitmap object

Upvotes: 2

Related Questions