Read pdf file from SD card

I want to read a pdf file stored on my SD card, I tried using this snippet

    File file = new File(Environment.getExternalStorageDirectory()
            + "/vvveksperten" + "/ypc.pdf");

    PackageManager packageManager = getPackageManager();
    Intent testIntent = new Intent(Intent.ACTION_VIEW);
    testIntent.setType("application/pdf");
    List list = packageManager.queryIntentActivities(testIntent,
            PackageManager.MATCH_DEFAULT_ONLY);
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    Uri uri = Uri.fromFile(file);
    intent.setDataAndType(uri, "application/pdf");
    startActivity(intent);

      

but that gave me an error.

ERROR/AndroidRuntime(2611): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/vvveksperten/ypc.pdf typ=application/pdf }

      

+3


source to share


4 answers


Please check that any PDF reader application is available on your device, I think it is not.

Just use this code,



private void viewPdf(Uri file) {
        Intent intent;
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(file, "application/pdf");
        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            // No application to view, ask to download one
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("No Application Found");
            builder.setMessage("Download one 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();
        }
    }

      

If any pdf reader app is not available, this code will download pdf reader from android market, but make sure you have android market app installed on your device . So I think try this on an android device and not an emulator.

+5


source


Follow this link for https://github.com/jblough/Android-Pdf-Viewer-Library to read PDF files without using third party apps.



+7


source


    File file = new File("/sdcard/read.pdf");

Intent intent = new Intent(Intent.ACTION_VIEW);

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

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

startActivity(intent);

      

+1


source


Many (or maybe most) Android devices do not come with a default PDF viewer, unlike iOS devices. This is why you are getting an exception. There is no intent registered with the correct IntentFilter.

Also see this other SO question: Does android have a built-in PDF viewer? ...

The solution is simple: install a PDF viewer like Adobe Reader from Google Play.

0


source







All Articles