Manoj MM
Manoj MM

Reputation: 490

Opening pdf file through Intent gives error

I want to open pdf file which i have already downloaded on my sd card. But while opening that file through Intent It gives me error as No activity found to handle this intent. I want to open pdf in my application through pdf reader or any other way. Suggest me better solution to open pdf if possible.

    PackageManager packageManager = getPackageManager();
    Intent pdf= new Intent(Intent.ACTION_MAIN);
                                    pdf.addCategory("android.intent.category.LAUNCHER");
                                    pdf.setPackage("com.adobe.reader");
                                    pdf.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                    pdf.setDataAndType(Uri.fromFile(pdfPath),"application/pdf");
pdf.setData(Uri.parse( Environment.getExternalStorageDirectory().toString() + "/downloadedfile.pdf"));

Please help me with this.

Upvotes: 1

Views: 1078

Answers (1)

Manoj MM
Manoj MM

Reputation: 490

Hey I found solution here it is.. I am opening it in Adobe pdf reader. If adobe reader is not installed i am redirecting to google play.

private void viewPdf(Uri file){
    Intent intent;
    intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(file, "application/pdf");
    try{
    startActivity(intent);
    }catch(ActivityNotFoundException e){
    AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
    builder.setTitle("No Application Found");
    builder.setMessage("Download from Android Market?");
    builder.setPositiveButton("Yes, Please", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub

    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();

    }

Upvotes: 1

Related Questions