Thursday, 14 March 2013

How to Read PDF files in Android?

Read PDF Files from Sdcard in Android

First Create one Android Project in Eclipse after that download PDFViewer.jar file from Internet and then add into project's build path.

After Create one Activity in this Project, Name is Second.java

Second.java

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.left_arrow;
    }
   
    public int getNextPageImageResource() {
    return R.drawable.right_arrow;
    }
   
    public int getZoomInImageResource() {
    return R.drawable.zoom_in;
    }
   
    public int getZoomOutImageResource() {
    return R.drawable.zoom_out;
    }
   
    public int getPdfPasswordLayoutResource() {
    return R.layout.pdf_file_password;
    }
   
    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;
    }
}

After that Add below code into your project's main activity and change extends Activity to ListActivity in your java file.

First.java

public class First extends ListActivity {
   
    String[] pdflist;
    File[] imagelist;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);
   
    File images = Environment.getExternalStorageDirectory();
    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(First.this, Second.class);
    intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);
    startActivity(intent);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
}

And Don't Forget to Add Second Activity in Android Manifest file.

Enjoy :--)

14 comments:

  1. how read assets floder pfd file

    ReplyDelete
  2. Can you Please send me source code of this project?
    I got error in it...
    My id-bkothari99@gmail.com

    ReplyDelete
  3. i got a error in onclick..help me..

    ReplyDelete
  4. i also got error when m click on file.......

    ReplyDelete
  5. Replies
    1. https://github.com/jblough/Android-Pdf-Viewer-Library

      Delete
  6. Dear Sir,

    Thank you sir for this post. i am getting List Books from my mnt/sdcard. but when i click on any book it shows me error like this. [ http://pastebin.com/TUaVKrxt ].
    and it shows error on below two line from first.java class.
    " openPdfIntent(path);" //line no.53
    "final Intent intent = new Intent(First.this, Second.class);" //line no. 58

    PLEASE HELP ME AS SOON AS POSSIBLE.

    ReplyDelete
  7. HI Dipak,Please send me android custom pdf viewer source code it will will helpfull for me

    ReplyDelete
  8. it is run but some time it crash automatically..

    ReplyDelete
  9. Everything is fine but only Loading PDF Page, and PDF is not loading...

    ReplyDelete
  10. This is a great article but very debatable. Im going to say that Steve Jobs absolutely loves the App store simply because it has been such a huge part of the iPhones success. It has made the iPhone so much more appealing simply because of all the awesome apps that developers top gadgets reviews online have created for Steve Jobs phone, the iPhone.

    ReplyDelete
  11. I have come across better blog writers who are capable of holding the attention of their readers. You can check out some really awesome blogs at www.zopthemes.com. As I can clearly make out your amateurish content, you can brush up your writing skills with the help of good blogging tips at sell my creative graphic design,superb logo design. Hey…no personal feelings. Just wanted to help.

    ReplyDelete
  12. A great online marketing company for technical support inbound calls & pop up calls for tech support , PPC Expert For Tech Support by osiel web.

    ReplyDelete
  13. A great online marketing company for technical support inbound calls & pop up calls for tech support , PPC Expert For Tech Support by osiel web.

    ReplyDelete