java - 如何从应用内浏览器打开 PDF 文件?

标签 java android pdf android-intent browser

我的应用程序中有一个基本的文件浏览器,我希望它能够打开 PDF 文件,但由于某种原因,即使我可以点击 PDF 文件并打开一秒钟,它们也会消失,我的手机给出一 strip 有错误的 Toast 消息,并且我没有在代码中的任何地方编写此 Toast。这很奇怪,因为从我的其他 Activity 中打开同一个 PDF 文件没有任何问题。 (请参阅适用于我的 PDFManager 类的 OpenPDF() 方法)我研究了很多有关如何解决此问题的信息,但找不到明确的答案。感谢您的帮助!

The toast error: Cannot display PDF (filename.pdf cannot be opened)

OnListItemClick function from AndroidFileBrowser:

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        String filename = (String) getListAdapter().getItem(position);
        if (path.endsWith(File.separator)) {
            filename = path + filename;
        } else {
            filename = path + File.separator + filename;
        }
        pdf = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/PATH/" + filename);
        Uri path = Uri.fromFile(pdf);
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(path, "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
        e.printStackTrace();
        }
    }

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.faraz.pdfreader"
          xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application

        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".SimpleScannerActivity"
            android:label="@string/simple_scanner_activity"
            android:theme="@style/AppOverlayTheme">
        </activity>
        <activity android:name=".PDFManager"/>
        <activity android:name=".AndroidFileBrowser"/>
                  <!--android:screenOrientation="portrait"-->
    </application>
</manifest>

我的 PDFManager 类中的 OpenPDF() 方法

    public void openPDF() {
        File pdf = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/PATH/" + pdfname + ".pdf");
        if (pdf.exists()) {
            Uri path = Uri.fromFile(pdf);
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setDataAndType(path, "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            try {
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(PDFManager.this,
                        "No application available to view PDF!",
                        Toast.LENGTH_SHORT).show();
     /*                Intent intent = new Intent(Intent.ACTION_VIEW)
                        .setData(Uri.parse("http://docs.google.com/gview?embedded=true&url=" + url));
                startActivity(intent);
            }
     */
            }
        }
    }
 }

最佳答案

在您的 OnItemClick() 中:

if (path.endsWith(File.separator)) 
{
    filename = path + filename;
} 
else 
{
    filename = path + File.separator + filename;
}

pdf = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/PATH/" + filename);
Uri path = Uri.fromFile(pdf);

您在初始化“路径”之前使用它,因此根据您的 Intent ,可以更改其中一个或另一个的名称,或者在 if 语句之前而不是之后声明它。

关于java - 如何从应用内浏览器打开 PDF 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31396203/

相关文章:

java - for 循环中的 JTextField 数组将自身添加到布局中

java - 配置 log4j org.apache.log4j.rolling.RollingFileAppender,以便在日志数量超过设置的索引量后删除压缩日志

java - 当其他打开时卡住 JFrame

android - 关闭通知后打开应用程序主页

android - 如何在手机上安装特定版本的 Android 进行测试?

python - 使用 Python 或 Bash 删除 PDF 的最后一页

javascript - 如何使用 jsPDF 合并两个 PDF 文件

java - 如何在测试方法中使用expectedExceptionsMessageRegExp

android - 我们如何在主键盘页面的焦点上显示@字符键盘而不需要按123键盘来获取?

pdf - DataDynamics.ActiveReports.Export.Pdf.PdfExport 引用的语言编码问题