簡體   English   中英

無法在android中以編程方式通過意圖打開PDF文件

[英]Unable to open PDF file through intent programmatically in android

我有一個應用程序,點擊按鈕,應該提供訪問權限以瀏覽和打開存儲在設備內部存儲器中的 PDF 文件。我嘗試了各種代碼,但我祝酒詞說“無法顯示 PDF”。 我在日志中沒有收到任何錯誤。 我使用 android 版本 7.0 設備。 下面附上我的代碼:請幫助:

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

我已將此包含在 Oncreate 中:

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

這就是調用方法的方式:

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

試試這個...

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

調用此方法:

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

打開一個特定文件:

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

檢查目錄,這個有效。

要從本地存儲打開 pdf 文件,請在 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();
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM