android: BufferUnderflowException while rendering pdf

470 views Asked by At

I am trying to implement a program to read pdf file but when I am doing so I am not able to show the pages in first time and in second time its showing few pages with overlapping words.I dont understand what is wrong Could anyone suggest me what to do?

public class MainActivity extends ListActivity {

String[] pdflist;
File[] imagelist;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.main);

    File images = Environment.getExternalStorageDirectory();
    System.out.println("images" + images);
    imagelist = images.listFiles(new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return ((name.endsWith(".pdf")));
        }
    });
    pdflist = new String[imagelist.length];
    for (int i = 0; i < imagelist.length; i++) {
        pdflist[i] = imagelist[i].getName();
    }
    this.setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, pdflist));
}

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    String path = imagelist[(int) id].getAbsolutePath();
    openPdfIntent(path);
}

private void openPdfIntent(String path) {
    try {
        final Intent intent = new Intent(MainActivity.this, Second.class);
        intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);
        startActivity(intent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

Logcat details:

02-04 13:14:09.232: E/PDFVIEWER(1123): null
02-04 13:14:09.232: E/PDFVIEWER(1123): java.nio.BufferUnderflowException
02-04 13:14:09.232: E/PDFVIEWER(1123):  at java.nio.Buffer.checkGetBounds(Buffer.java:177)
02-04 13:14:09.232: E/PDFVIEWER(1123):  at java.nio.DirectByteBuffer.get(DirectByteBuffer.java:41)
02-04 13:14:09.232: E/PDFVIEWER(1123):  at java.nio.MappedByteBufferAdapter.get(MappedByteBufferAdapter.java:144)
02-04 13:14:09.232: E/PDFVIEWER(1123):  at java.nio.ByteBuffer.get(ByteBuffer.java:384)
02-04 13:14:09.232: E/PDFVIEWER(1123):  at net.sf.andpdf.nio.NioByteBuffer.get(NioByteBuffer.java:99)
02-04 13:14:09.232: E/PDFVIEWER(1123):  at com.sun.pdfview.decode.FlateDecode.decode(FlateDecode.java:68)
02-04 13:14:09.232: E/PDFVIEWER(1123):  at com.sun.pdfview.decode.PDFDecoder.decodeStream(PDFDecoder.java:92)
02-04 13:14:09.232: E/PDFVIEWER(1123):  at com.sun.pdfview.PDFObject.decodeStream(PDFObject.java:334)
02-04 13:14:09.232: E/PDFVIEWER(1123):  at com.sun.pdfview.PDFObject.getStreamBuffer(PDFObject.java:307)
02-04 13:14:09.232: E/PDFVIEWER(1123):  at com.sun.pdfview.PDFFile.dereference(PDFFile.java:272)
02-04 13:14:09.232: E/PDFVIEWER(1123):  at com.sun.pdfview.PDFObject.dereference(PDFObject.java:686)
02-04 13:14:09.232: E/PDFVIEWER(1123):  at com.sun.pdfview.PDFObject.getDictRef(PDFObject.java:508)
02-04 13:14:09.232: E/PDFVIEWER(1123):  at com.sun.pdfview.PDFFile.findPage(PDFFile.java:1696)
02-04 13:14:09.232: E/PDFVIEWER(1123):  at com.sun.pdfview.PDFFile.getPage(PDFFile.java:1569)
02-04 13:14:09.232: E/PDFVIEWER(1123):  at net.sf.andpdf.pdfviewer.PdfViewerActivity.showPage(PdfViewerActivity.java:779)
02-04 13:14:09.232: E/PDFVIEWER(1123):  at net.sf.andpdf.pdfviewer.PdfViewerActivity.access$12(PdfViewerActivity.java:769)
02-04 13:14:09.232: E/PDFVIEWER(1123):  at net.sf.andpdf.pdfviewer.PdfViewerActivity$3.run(PdfViewerActivity.java:259)
02-04 13:14:09.232: E/PDFVIEWER(1123):  at java.lang.Thread.run(Thread.java:856)

Second class :

package com.syntel.pdfviewer;

import android.os.Bundle;
import net.sf.andpdf.pdfviewer.PdfViewerActivity;

public class Second extends PdfViewerActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
}

public int getPreviousPageImageResource() {
    return R.drawable.ic_launcher;
}

public int getNextPageImageResource() {
    return R.drawable.ic_launcher;
}

public int getZoomInImageResource() {
    return R.drawable.ic_launcher;
}

public int getZoomOutImageResource() {
    return R.drawable.ic_launcher;
}

@Override
public int getPdfPageNumberEditField() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public int getPdfPageNumberResource() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public int getPdfPasswordEditField() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public int getPdfPasswordExitButton() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public int getPdfPasswordLayoutResource() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public int getPdfPasswordOkButton() {
    // TODO Auto-generated method stub
    return 0;
}

/*
 * public int getPdfPasswordLayoutResource() { return R.layout.ic_launcher;
 * }
 * 
 * public int getPdfPageNumberResource() { return
 * R.layout.dialog_pagenumber; }
 * 
 * public int getPdfPasswordEditField() { return R.id.etPassword; }
 * 
 * public int getPdfPasswordOkButton() { return R.id.btOK; }
 * 
 * public int getPdfPasswordExitButton() { return R.id.btExit; }
 * 
 * public int getPdfPageNumberEditField() { return R.id.pagenum_edit; }
 */
}
1

There are 1 answers

15
Chintan Soni On

Try this:

lets use PDFViewer.jar file from below location:

http://www.filecrop.com/pdf-viewer.jar.html

Just let me know if it worked for you.

Update:

If you want to load file from asset folder, simply get the path to that folder like:

File mFile = new File("file:///android_asset/yourFilename.pdf");