簡體   English   中英

從SD卡讀取pdf文件

[英]Read pdf file from sd card

我想閱讀存儲在我的SD卡中的pdf文件,我嘗試使用這個片段

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

但它給了我一個錯誤。

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 }

點擊此鏈接https://github.com/jblough/Android-Pdf-Viewer-Library for pdf reader,無需使用任何第三方應用程序。

請檢查您的設備是否有任何pdf閱讀器應用程序,我認為不是..

只需使用此代碼,

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

如果任何pdf閱讀器應用程序不可用,那么此代碼是從Android市場下載pdf閱讀器,但請確保您的設備已預安裝Android市場應用程序。 所以我認為在Android設備上嘗試這個,而不是模擬器。

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

與iOS設備不同,許多(或許是大多數)Android設備沒有默認的PDF查看器。 這就是為什么你得到了例外。 沒有使用正確的IntentFilter注冊Intent。

另請看其他問題: android有內置的PDF查看器嗎?

解決方案很簡單:從Google Play安裝Adobe Reader等PDF閱讀器。

暫無
暫無

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

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