2014-10-12 92 views
1

我用github的pdf查看示例並嘗試運行。它不會顯示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文件。

回答

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); 
0

這會工作:

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