无法在我的应用程序中使用Barteksc PDF Viewer

时间:2018-10-04 12:11:38

标签: android

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

[1]:在我的Android应用程序中为https://github.com/barteksc/AndroidPdfViewer,可从该应用程序内的资产文件夹中查看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

以下是我的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'
}

2 个答案:

答案 0 :(得分:1)

问题在于,有必要在PDFView对象上调用.load()方法以显示pdf。因此,在java文件中,应该是这样

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

不仅限于:

pdfView.fromAsset("sample_file.pdf");

答案 1 :(得分:0)

您发布的代码缺少读取存储所需的权限。

在github上有一个带有您发布的库的演示。检查请求读取存储权限的代码并在您的代码中实现。

您可以参考库随附的示例示例类。

https://github.com/barteksc/AndroidPdfViewer/blob/master/sample/src/main/java/com/github/barteksc/sample/PDFViewActivity.java