AndroidPDFViewer - 无法在我的应用程序中打开 pdf 文档

AndroidPDFViewer - Can not open pdf document in my application

我正在使用 this 库。我正在尝试加载位于资产中的 PDF,但它没有加载。这是我加载它的地方:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.github.barteksc.pdfviewer.PDFView;

public class MainActivity extends AppCompatActivity {

    private PDFView pdfView;
    private String nameOfPDF = "" + R.string.name_of_pdf;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pdfView = (PDFView) findViewById(R.id.pdfView);

        pdfView.fromAsset(nameOfPDF).load();
    }
}

这里是相关的 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ru.webant.projects.mypdfviewer.MainActivity">

    <com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

应用程序 运行 但没有任何反应。为什么会这样?

private String nameOfPDF = "" + R.string.name_of_pdf;

除非您碰巧将您的资产命名为一些大的 semi-random 数字,否则这是不正确的。

如果 R.string.name_of_pdf 被假定为具有您的 PDF 文件名称的字符串资源的标识符,请使用:

private String nameOfPDF = getString(R.string.name_of_pdf);

This sample project 演示了如何使用 AndroidPdfViewer 显示资产中的 PDF 以及其他地方。

因为你没有设置正确的文件名。看这里:

private String nameOfPDF = "" + R.string.name_of_pdf;

结果为:nameOfPDF = SomeIdAsString。你要调用的是 getString() 像这样:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    pdfView = (PDFView) findViewById(R.id.pdfView);

    pdfView.fromAsset(getString(R.string.name_of_pdf)).load();
}