0

I am trying to open a pdf file via an already installed pdf-viewer in android.

With reference to link "android: open a pdf from my app using the built in pdf viewer"

Here is the sample code :

private void openPDF(String filePath)
{
    Intent target = new Intent(Intent.ACTION_VIEW);
    target.setDataAndType(Uri.parse(filePath),"application/pdf");
    target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

    Intent intent = Intent.createChooser(target, "Open File");
    if(intent!=null)
    {
        try {
            startActivity(intent); // Here it giving null pointer exception 
        }
        catch (ActivityNotFoundException e) {
        // Instruct the user to install a PDF reader here, or something
        }
    }
    else
        Log.d("DEBUG","Intent is null");
} 

But I am getting a null pointer exception on startActivity(intent) line.

Please help me to find the issue.

Stack Trace :

01-28 16:24:48.865: W/System.err(4399): java.lang.NullPointerException
01-28 16:24:48.875: W/System.err(4399):     at android.app.Activity.startActivityForResult(Activity.java:3370)
01-28 16:24:48.875: W/System.err(4399):     at android.app.Activity.startActivityForResult(Activity.java:3331)
01-28 16:24:48.875: W/System.err(4399):     at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:819)
01-28 16:24:48.875: W/System.err(4399):     at android.app.Activity.startActivity(Activity.java:3566)
01-28 16:24:48.875: W/System.err(4399):     at android.app.Activity.startActivity(Activity.java:3534)
01-28 16:24:48.875: W/System.err(4399):     at a.MainActivity.openPDF(MainActivity.java:591)
01-28 16:24:48.875: W/System.err(4399):     at a.MainActivity.access$0(MainActivity.java:581)
01-28 16:24:48.885: W/System.err(4399):     at a.MainActivity$TestSectionFragment$1.onItemClick(MainActivity.java:480)
Jeet
  • 115
  • 2
  • 13

3 Answers3

1

Do you try?

 Intent intent = new Intent();                      
 intent.setClassName("com.adobe.reader", "com.adobe.reader.AdobeReader");
 intent.setAction(android.content.Intent.ACTION_VIEW);
 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

 Uri uri = Uri.fromFile(file);
 intent.setDataAndType(uri, "application/pdf");

try {                               
    startActivity(intent);
    } catch (Exception e) {

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("No Application Found");
builder.setMessage("Download Application from Android Market?");
builder.setPositiveButton("Yes, Please",new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog,int which) {

        Intent marketIntent = new Intent(Intent.ACTION_VIEW);
        marketIntent.setData(Uri.parse("market://details?id=com.adobe.reader"));

        startActivity(marketIntent);
        }
    });
    builder.setNegativeButton("No, Thanks",null);
    builder.create().show();
    }
M D
  • 47,665
  • 9
  • 93
  • 114
  • When I used the code in another activity and started the activity, it was working, But with an activity, you get a view part as well. I dont want that, So I moved the code to same file. Then it started crashing. – Jeet Jan 28 '15 at 11:27
  • @Jeet I don't understand. give me brief details – M D Jan 28 '15 at 11:29
  • Previously I wrote the same code in other class extending Activity, Then I moved the same code in the MainActivity itself. Here it started crashing. – Jeet Jan 28 '15 at 11:34
  • @Jeet Crash log plz. show me line no `480` in `MainActivity.java` and check your `filePath` – M D Jan 28 '15 at 11:35
  • Got the issue. It was because I was trying to create MainActivity object and then calling the method written in the class. Is there any issue with that approach? – Jeet Jan 28 '15 at 12:08
0

Hope it helps,

Intent intent = new Intent(Intent.ACTION_VIEW,
        Uri.parse("path-to-document"));
intent.setType("application/pdf");
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
if (activities.size() > 0) {
    startActivity(intent);
} else {
    // Do something else here. Maybe pop up a Dialog or Toast
}

Please try this link: http://stackoverflow.com/questions/10299839/how-to-read-pdf-in-my-android-application

Anitha
  • 253
  • 4
  • 16
0

Replace below line:

 startActivity(intent);

with:

if (intent.resolveActivity(context.getPackageManager()) != null) {
    startActivity(intent);
}
Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61