android - 无法在我的应用程序中使用 barteksc PDF 查看器

标签 android

我尝试使用 [bartecksc AndroidPdfViewer][1]

[1]: https://github.com/barteksc/AndroidPdfViewer在我的 android 应用程序中查看应用程序内 Assets 文件夹中的 PDF 文件,但它不起作用。它显示一个空白屏幕。 这是我所做的: 我在 build.gradle 文件中添加了这个依赖

implementation 'com.github.barteksc:android-pdf-viewer:3.1.0-beta.1'

(我什至尝试使用 implementation 'com.github.barteksc:android-pdf-viewer:2.8.2')

我在 activity_main.xml 中添加了以下小部件

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

我把下面的代码放在onCreate方法中

PDFView pdfView=findViewById(R.id.pdfView);
        pdfView.fromAsset("sample_file.pdf");

我想要显示的文件位于名为 sample_file.pdf 的 Assets 文件夹中

以下是我的MainActivity.java

package com.umersoftwares.bartekscpdfview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.github.barteksc.pdfviewer.PDFView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PDFView pdfView=findViewById(R.id.pdfView);
        pdfView.fromAsset("sample_file.pdf");
    }
}

以下是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

</android.support.constraint.ConstraintLayout>

我的 build.gradle 文件是:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.umersoftwares.bartekscpdfview"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
}

最佳答案

问题是需要调用 PDFView 对象的 .load() 方法来显示 pdf。所以,在java文件中,应该是这样的

pdfView.fromAsset("sample_file.pdf")
.load();

而不是仅仅:

pdfView.fromAsset("sample_file.pdf");

关于android - 无法在我的应用程序中使用 barteksc PDF 查看器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52646401/

相关文章:

Android Places Autocomplete 设置经纬度范围

android - 设备屏幕休眠时测试失败

android - RXJava Observable 在主线程而不是后台线程上执行

android - Retrofit2 请求Body前面不同括号的奇怪组合

android - 基于 MIPS 的 Android 免费 Pascal

Android:推送通知点击后打开外部浏览器

java - 如何从 Intent 对象android获取请求代码

Android 自定义创建的 xml 按钮未显示在预览中

android - 将 Arraylist 作为 json 对象的值发送?

java - 不幸的是 MyApp 已停止。我该如何解决这个问题?