4

I have download pdf files in my sdcard but when i click them to open in my emulator it gives a toast

"Cannot open file" please help me to read the pdf file .... Please tell me what is wrong in this code.....

File file=new File("/sdcard/Android.Essentials.pdf");
 if (file.exists())
 {
Uri path = Uri.fromFile(file);
 Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
try {

  startActivity(intent);
   finish();
}
catch (ActivityNotFoundException e) {
Toast.makeText(PdffileActivity.this,
"No Application Available to View PDF",
 Toast.LENGTH_SHORT).show();
 }
Amit Parjapati
  • 61
  • 1
  • 2
  • 10
  • Why you are calling startActivity(intent); two times .one before try block and one inside try block. – Vipul Jun 05 '12 at 10:57

3 Answers3

4

To launch an Intent on a PDF file, the mobile device should have installed a program that can read PDF documents.

If you do not want the user to have a PDF reader's installed, you must modify your application to be able to read PDF documents.

On this post talk about PDF libraries for Android: Android : Is there any free PDF library for Android

Community
  • 1
  • 1
Lobo
  • 4,001
  • 8
  • 37
  • 67
  • :i also added the lib named folder containing all iText jar files parallel to the src folder .. still i am not able to read .... – Amit Parjapati Jun 05 '12 at 10:53
  • @AmitParjapati To use the iText library you should not open the PDF document with a startIntent. Need to open following the iText library documentation. – Lobo Jun 05 '12 at 11:02
3

You need to install any PDF Reader App in your emulator.

Your intent is looking for any installed PDF reader and in your emulator may be there is no PDF reader installed.

Just install this app in your emulator

Mihir Palkhiwala
  • 2,586
  • 3
  • 37
  • 47
2

Just download apk from this link to view Pdf files on emulator.

PdfViewerApk for Emulator

jazzbpn
  • 6,441
  • 16
  • 63
  • 99