我正在使用这库。我正在尝试加载位于资产中的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>应用程序正在运行,但什么也不会发生。为什么会发生这种情况?
发布于 2017-02-16 14:32:47
因为您没有设置正确的文件名。看这里:
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();
}发布于 2017-02-16 14:30:58
private String nameOfPDF = "" + R.string.name_of_pdf;除非你碰巧把你的资产命名为某个大的半随机数,否则这是不正确的。
如果R.string.name_of_pdf应该是具有PDF文件名称的字符串资源的标识符,请使用:
private String nameOfPDF = getString(R.string.name_of_pdf);此示例项目演示了如何使用AndroidPdfViewer显示来自资产的PDF,以及其他地方。
https://stackoverflow.com/questions/42276728
复制相似问题