ActivityNotFoundException always called for PDF Viewer

847 views Asked by At

I'm having this problem where I'm trying to open a PDF file in my app, if there are no PDF viewers on the device it redirects to the Play Store to download Adobe Reader. However I already downloaded Adobe Reader, it still catches ActivityNotFoundException.

Here's my code:

            Uri uri = Uri.parse(pdfUrl);
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(uri, "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            try {
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                // No application to view, ask to download one
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("No Viewer");
                builder.setMessage("Download PDF Viewer?");
                builder.setPositiveButton(getString("Okay"),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                Intent innerIntent = new Intent(
                                        Intent.ACTION_VIEW);
                                innerIntent.setData(Uri
                                        .parse("market://details?id=com.adobe.reader"));
                                startActivity(innerIntent);
                            }
                        });
                builder.setNegativeButton("Cancel"), null);
                builder.create().show();
            }

I already have downloaded the Adobe reader, the next time I run the app it still prompts me the dialog to download a PDF Viewer. What's the reason behind this?

2

There are 2 answers

0
Mohammed Saleem On

Try this Code:

try {


                PackageManager packageManager = getActivity()
                        .getPackageManager();
                Intent testIntent = new Intent(Intent.ACTION_VIEW);
                testIntent.setType("application/pdf");
                List<?> list = packageManager.queryIntentActivities(testIntent,
                        PackageManager.MATCH_DEFAULT_ONLY);

                if (list.size() > 0 && file.isFile()) {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    Uri uri = Uri.fromFile(file);
                    intent.setDataAndType(uri, "application/pdf");
                    startActivity(intent);
                } else {

            Toast.makeText(this, "PDF Reader application is not installed in your device", Toast.LENGTH_LONG)
                .show();

                    }
                }

            } catch (NullPointerException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();


    }
0
ewilly On

I think your problem comes frome the source of your file. Your can be on internet so you have a link (http : // link_to_pdf) , your file can be local and in this case (In your project resources like asset for example or in the SDcard)

The code you're using now is advised for local file on the SDcard or local in the physical device.

For the web file try to use google docs viewer.

The link for the source code is here