Open PDF in Android App

I am working on an application where I need to open a pdf file on a device,

I really got code on the internet that is similar to most examples. But the fact is, I can’t open the file, and the control goes directly to the Exception part.

Here is the code below:

public class MyPDFDemo extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button OpenPDF = (Button) findViewById(R.id.button); OpenPDF.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { File pdfFile = new File("/sdcard/Determine_RGB_Codes_With_Powerpoint [PDF Library].pdf"); if(pdfFile.exists()) { Uri path = Uri.fromFile(pdfFile); Intent pdfIntent = new Intent(Intent.ACTION_VIEW); pdfIntent.setDataAndType(path, "application/pdf"); pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); try { startActivity(pdfIntent); } catch(ActivityNotFoundException e) { Toast.makeText(MyPDFDemo.this, "No Application available to view pdf", Toast.LENGTH_LONG).show(); } } } }); } 

When I run this code: I used to view "There is no application to view pdf." Can I view a pdf file for someone.

+16
android pdf
May 21 '11 at 5:54 am
source share
3 answers

Since your catch block has an ActivityNotFoundException, this means that you do not have an activity / application that can read the application / pdf file format. Install any pdf viewer from the Android Market (recently release Adobe), or use the aforementioned open source viewer, and your problem will most likely be solved.

http://code.google.com/p/apv/downloads/list

https://market.android.com/details?id=cx.hell.android.pdfview&feature=search_result

When you start working with your parameters, she searches for all applications / actions / intentions registered to open the pdf format. Since you do not have them on your device, you get an ActivityNotFoundException

+9
Jun 06 '11 at 8:32
source share

Your code is right, I also used the same code to open the PDF file in the viewer.

Since you do not have a viewer installed on your device, therefore, it cannot be opened without any viewer.

You can install Adobe reader for Android.

I can not open the PDF file in the emulator, so I need to test it using my device.

+2
Nov 01 '11 at 10:09
source share

First install the PDF reader on the device. than using this code to read a pdf file from internal memory.

  protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); final TextView tv = (TextView)findViewById(R.id.tv); Button bt=(Button)findViewById(R.id.openbtn); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { File pdfFile = new File(Environment.getExternalStorageDirectory(),"Leave.pdf"); if(pdfFile.exists()) { Uri path = Uri.fromFile(pdfFile); Intent pdfIntent = new Intent(Intent.ACTION_VIEW); pdfIntent.setDataAndType(path, "application/pdf"); pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); try{ startActivity(pdfIntent); }catch(ActivityNotFoundException e){ tv.setText("No Application available to view PDF"); } } else { tv.setText("File not found"); } } }); 

}

+2
May 26 '16 at 6:10
source share



All Articles