它没有显示pdf文件

时间:2014-10-12 10:43:05

标签: android pdf

我使用了来自github的pdf视图示例并尝试运行。

https://github.com/JoanZapata/android-pdfview

下面是主要的课程

package com.joanzapata;

import android.content.Intent;
import android.util.Log;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.googlecode.androidannotations.annotations.*;
import com.joanzapata.pdfview.PDFView;
import com.joanzapata.pdfview.listener.OnPageChangeListener;


import static java.lang.String.format;


public class PDFViewActivity extends SherlockActivity implements OnPageChangeListener {

    public static final String SAMPLE_FILE = "sample.pdf";

    public static final String ABOUT_FILE = "about.pdf";

    @ViewById
    PDFView pdfView;

    @NonConfigurationInstance
    String pdfName = SAMPLE_FILE;

    @NonConfigurationInstance
    Integer pageNumber = 1;

    @AfterViews
    void afterViews() {
        display(pdfName, false);
    }

    @OptionsItem
    public void about() {
        if (!displaying(ABOUT_FILE))
            display(ABOUT_FILE, true);
    }

    private void display(String assetFileName, boolean jumpToFirstPage) {
        if (jumpToFirstPage) pageNumber = 1;
        setTitle(pdfName = assetFileName);

        pdfView.fromAsset(assetFileName)
                .defaultPage(pageNumber)
                .onPageChange(this)
                .load();
    }

    @Override
    public void onPageChanged(int page, int pageCount) {
        pageNumber = page;
        setTitle(format("%s %s / %s", pdfName, page, pageCount));
    }

    @Override
    public void onBackPressed() {
        if (ABOUT_FILE.equals(pdfName)) {
            display(SAMPLE_FILE, true);
        } else {
            super.onBackPressed();
        }
    }

    private boolean displaying(String fileName) {
        return fileName.equals(pdfName);
    }
}

我已经将actionbarsherlock和androidannotations-2.5-api jar文件附加到project.This运行没有任何错误,但它没有显示pdf文件。

2 个答案:

答案 0 :(得分:0)

你有一个名为" sample.pdf"的文件。在您的资产文件夹? Where do I place the 'assets' folder in Android Studio?

您也可以尝试从SD卡读取文件:

    file = new File(path); // where "path" is a path to file on your sd card, example: /storage/sdcard0/yourFile.pdf
    filename = file.getName();
    setTitle(filename);
    pdfView  =(PDFView)findViewById(R.id.pdfView); 
    pdfView.fromFile(file).defaultPage(1).showMinimap(false).enableSwipe(true).swipeVertical(true).load();
    pdfView.setVerticalScrollBarEnabled(true);

答案 1 :(得分:0)

这样可行:

pdfView = (PDFView) findViewById(R.id.pdfView);
pdfView.fromFile(new File(path)).defaultPage(pageNumber).onPageChange(this).load();