简体   繁体   中英

Unable to open PDF file through intent programmatically in android

I have an app where on button click, access should be provided to browse and open the PDF files stored in the internal memory of the device.I tried various codes but i get an toast saying "Cannot display PDF". I don't get any errors in logs. I use android version 7.0 device. Attached my code below: please help:

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent); 

I have included this in Oncreate:

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build()); 

This is how called the method:

  linear_pdf.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            pdf.setTextColor(getActivity().getResources().getColor(R.color.colorWhite));


                            openPdf(getContext(), Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");



                            communication.dismiss();
                        }
                    });

Try this one...

public void openPdf(Context context, String path){
            File file = new File(path);
            if (file.exists()) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(file), "application/pdf");
                PackageManager pm = context.getPackageManager();
                Intent sendIntent = new Intent(Intent.ACTION_VIEW);
                sendIntent.setType("application/pdf");
                Intent openInChooser = Intent.createChooser(intent, "Choose");
                List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
                if (resInfo.size() > 0) {
                    try {
                        context.startActivity(openInChooser);
                    } catch (Throwable throwable) {
                        Toast.makeText(context, "PDF apps are not installed", Toast.LENGTH_SHORT).show();
                        // PDF apps are not installed
                    }
                } else {
                    Toast.makeText(context, "PDF apps are not installed", Toast.LENGTH_SHORT).show();
                }
            }
        }

Call this method:

openPdf(getApplicationContext(), Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");

For opening one specific file:

  File pdfFile = new File(Environment.getExternalStorageDirectory() + "/your_directory/" + "your_file_name" + ".pdf");
    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(getContext(), "No required app", Toast.LENGTH_SHORT).show();
    }

Check directory, this one works.

For opening pdf file from local storage add prefix in url.

private void openPdf(File file) {

        try {

            String url = file.getAbsolutePath();

            String u = "file:///" + url;

            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(u));
            startActivity(i);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM