Android app causing outofmemory error when opening pdf docs continuously
<p>I am new to android programming and trying to develop a simple pdf reader and for this I am using the following pdf viewer sdk
<a href="http://www.androidpdf.mobi/forum/Android-development/164-pdf-viewer-library" rel="nofollow">http://www.androidpdf.mobi/forum/Android-development/164-pdf-viewer-library</a></p>

<p>My code downloads the pdfs one by one when the next button is hit and shows them in the reader.
OnCreate method downloads a file and displays it in a view(which has next button in it) and from then when the nxt button is clicked it downloads the next file and displays it in the same view. </p>

<pre><code> case R.id.btn_next:
        {
            Log.e("PDFReaderAct", "next button clicked");
            if(next_index &lt;= urls.length-1)
            {
                                    //close the previous doc
                m_doc.Close();
               int ret = m_doc.Open(savedFiles.get(next_index).getAbsolutePath(),    "", "");

                showPdf();

                if(next_index &lt; urls.length-1){
                    next_index++;
                    saveFile(urls[next_index]);
                }
            prev_index++;
            }
            else{
                Toast.makeText(this, "You have reached last page", Toast.LENGTH_SHORT).show();
            }
        }
</code></pre>

<p>here is the showPdf method</p>

<pre><code>public void showPdf(){

    RelativeLayout lout = (RelativeLayout)LayoutInflater.from(this).inflate(R.layout.main, null);
    setContentView( lout );
    m_vPDF = (PDFReader)lout.findViewById(R.id.PDFView);

    m_vPDF.open(m_doc);

    m_vPDF.setViewListener(m_vPDF);
}
</code></pre>

<p>here is how I am downloading the files to externalFilesDir</p>

<pre><code>private class Downloader extends AsyncTask&lt;String, Void, String&gt; {

    File file;
    public Downloader(File file) {
        this.file = file;

    }
@Override
protected String doInBackground(String...params ) {

    InputStream is = null;
    OutputStream os = null;
        try {
            // Open InputStream to download the image. 
            Log.e("Downloader - URL", params[0]);
            is=new URL(params[0]).openStream();
            // Set up OutputStream to write data into image file. 
            os = new FileOutputStream(file);

            final int buffer_size=1024;
            try
            {
                byte[] bytes=new byte[buffer_size];
                for(;;)
                {
                  int count=is.read(bytes, 0, buffer_size);
                  if(count==-1)
                      break;
                  os.write(bytes, 0, count);
                }
            }
            catch(Exception ex){

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            try {
                if(is != null)
                    is.close();
                if(os != null)
                    os.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        return "success";
    }
    }
</code></pre>

<p>I can download and display 15 to 16 files before the app crashes resulting in outofmemory error. Unable to figure out the reason for this. </p>

<p>This is the exception I see..</p>

<pre><code>12-11 00:27:59.317: E/AndroidRuntime(528): FATAL EXCEPTION: main
12-11 00:27:59.317: E/AndroidRuntime(528): java.lang.OutOfMemoryError
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.graphics.Bitmap.nativeCreate(Native Method)
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.graphics.Bitmap.createBitmap(Bitmap.java:585)
12-11 00:27:59.317: E/AndroidRuntime(528):  at com.radaee.pdfex.PDFViewVert.viewResize(PDFViewVert.java:288)
12-11 00:27:59.317: E/AndroidRuntime(528):  at com.radaee.reader.PDFReader.onSizeChanged(PDFReader.java:112)
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.view.View.setFrame(View.java:11361)
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.view.View.layout(View.java:11272)
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.view.View.layout(View.java:11278)
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.view.View.layout(View.java:11278)
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.view.View.layout(View.java:11278)
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.view.View.layout(View.java:11278)
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.os.Looper.loop(Looper.java:137)
12-11 00:27:59.317: E/AndroidRuntime(528):  at android.app.ActivityThread.main(ActivityThread.java:4424)
12-11 00:27:59.317: E/AndroidRuntime(528):  at java.lang.reflect.Method.invokeNative(Native Method)
12-11 00:27:59.317: E/AndroidRuntime(528):  at java.lang.reflect.Method.invoke(Method.java:511)
12-11 00:27:59.317: E/AndroidRuntime(528):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-11 00:27:59.317: E/AndroidRuntime(528):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-11 00:27:59.317: E/AndroidRuntime(528):  at dalvik.system.NativeStart.main(Native Method)
</code></pre>

<p>Please let me know if more details are needed to help me on this.</p>
