How to clear java.lang.OutOfMemoryError in android PDFView?

676 Views Asked by At

friends i have created a pdfview in my app using webview successfully. But sometimes, i am facing crashes when i load pdf files with more pages like above 40, it will be helpfull if anyone of you guys can tell me why am i getting this message and what i have to do to avoid getting this error.Below is my logcat error message, which i got.

Logcat error:

Out of memory on a 1322872-byte allocation.
FATAL EXCEPTION: AsyncTask #2       
java.lang.RuntimeException: An error occured while executing doInBackground()  
    at android.os.AsyncTask$3.done(AsyncTask.java:300)    
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)    
    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)    
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)    
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)    
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)    
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)    
    at java.lang.Thread.run(Thread.java:841)    
Caused by: java.lang.OutOfMemoryError    
    at android.graphics.Bitmap.nativeCreate(Native Method)    
    at android.graphics.Bitmap.createBitmap(Bitmap.java:836)    
    at android.graphics.Bitmap.createBitmap(Bitmap.java:813)    
    at android.graphics.Bitmap.createBitmap(Bitmap.java:780)    
    at com.sun.pdfview.PDFPage.getImage(PDFPage.java:219)
    at android.os.AsyncTask$2.call(AsyncTask.java:288)    
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)

Below is my code:

        RandomAccessFile f = new RandomAccessFile(file, "r");
        data1 = new byte[(int)f.length()];
        f.readFully(data1);
        RandomAccessFile f = new RandomAccessFile(file, "r");
        byte[] data1 = new byte[(int)f.length()];
        f.readFully(data1)
        ByteBuffer bb = ByteBuffer.NEW(data1);
        PDFFile pdf = new PDFFile(bb);
        PDFPage PDFpage = pdf.getPage(1, true);
        final float scale = ViewSize / PDFpage.getWidth() * 0.95f; 
        float pdfWid = PDFpage.getWidth() * scale;
        float pdfH = PDFpage.getHeight() * scale;
        Bitmap page;
        if(page != null){
                 page.recycle();
                 page = null;
        }
        page = PDFpage.getImage((int)pdfWid, (int)pdfH, null, true, true);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        page.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        stream.reset();
        String base64 = Base64.encodeToString(byteArray, Base64.NO_WRAP);
        String html = "<!DOCTYPE html><html><body bgcolor=\"#b4b4b4\"><img src=\"data:image/png;base64,"+base64+"\" hspace=10 vspace=10><br>";
        int size = pdf.getNumPages();
                        try{
                        for(int i = 2; i <= size; i++)
                        {
                            PDFpage = pdf.getPage(i, true);
                            page = PDFpage.getImage((int)pdfWid, (int)pdfH, null, true, true);
                            page.compress(Bitmap.CompressFormat.PNG, 100, stream);
                            byteArray = stream.toByteArray();
                            stream.reset();
                            page.recycle();
                            base64 = Base64.encodeToString(byteArray, Base64.NO_WRAP);
                            html += "<img src=\"data:image/png;base64,"+base64+"\" hspace=10 vspace=10><br>";
                        }
                        }catch (Exception e){
                            Log.d("error", e.toString());
                        }
        stream.close();
        html += "</body></html>";
        wv.loadDataWithBaseURL("", html, "text/html","UTF-8", "");
1

There are 1 best solutions below

0
On BEST ANSWER

Well, i found what's the problem i was facing in my code, it was the heap size which caused the OutofMemory Exception. So, i just increased my heap size by declaring android:largeHeap="true" in Manifest which solved the issue.