如何在PdfViewerActivity中查看本地Pdf?

时间:2012-10-08 11:38:14

标签: android pdf

我正在使用此https://github.com/jblough/Android-Pdf-Viewer-Library 我在我的资源文件夹中放了6个pdf,并尝试从我的tabactivity中加载它们, 但它保持“加载PDF页面”,而我确定pdf文件不是那么大:P

这是我正在使用的代码:

public class MainActivity extends TabActivity {

@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tabHost = getTabHost();

setupTabHost();

}
private void setupTabHost() {
addTab("page1", R.drawable.thumbnail1, PdfActivity.class, 1);
addTab("page2", R.drawable.thumbnail2, PdfActivity.class, 2);
addTab("page3", R.drawable.thumbnail3, PdfActivity.class, 3);
addTab("page4", R.drawable.thumbnail4, PdfActivity.class, 4);
addTab("page5", R.drawable.thumbnail5, PdfActivity.class, 5);
addTab("page6", R.drawable.thumbnail6, PdfActivity.class, 6);
    }

private void addTab(String tag, int drawableId, Class<?> c, int pagenumber) {
//create tab
TabHost.TabSpec spec = tabHost.newTabSpec(tag);

    //set layout
View thumbnail = LayoutInflater.from(this).inflate(R.layout.thumbnail, getTabWidget(), false);
ImageView icon = (ImageView) thumbnail.findViewById(R.id.thumbnail_icon);
icon.setImageResource(drawableId);
    spec.setIndicator(thumbnail);

//add intent
Intent intent = new Intent(this, c);
                       intent.putExtra(net.sf.andpdf.pdfviewer.PdfViewerActivity.EXTRA_PDFFILENAME, "file:///android_asset/page"+pagenumber+".pdf");
spec.setContent(intent);

//add tab
    tabHost.addTab(spec);
     }
}


public class PdfActivity extends PdfViewerActivity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}



public int getPreviousPageImageResource() { return R.drawable.left_arrow; }
public int getNextPageImageResource() { return R.drawable.right_arrow; }
public int getZoomInImageResource() { return R.drawable.zoom_in; }
public int getZoomOutImageResource() { return R.drawable.zoom_out; }
public int getPdfPasswordLayoutResource() { return R.layout.pdf_file_password; }
public int getPdfPageNumberResource() { return R.layout.dialog_pagenumber; }
public int getPdfPasswordEditField() { return R.id.etPassword; }
public int getPdfPasswordOkButton() { return R.id.btOK; }
public int getPdfPasswordExitButton() { return R.id.btExit; }
public int getPdfPageNumberEditField() { return R.id.pagenum_edit; }

}

我的日志中已经有了这个,但我的资产文件夹中确实有page1.pdf!

10-09 16:55:00.223: I/PDFVIEWER(3650): onCreate
10-09 16:55:00.223: E/PDFVIEWER(3650): restoreInstance
10-09 16:55:00.308: D/dalvikvm(3650): GC_CONCURRENT freed 168K, 4% free 12979K/13383K, paused 10ms+2ms
10-09 16:55:00.373: I/PDFVIEWER(3650): Intent { cmp=xxxx (has extras) }
10-09 16:55:00.378: I/PDFVIEWER(3650): ST='file 'file:///android_asset/page1.pdf' not found'
10-09 16:55:00.383: I/PDFVIEWER(3650): ST='reading page 1, zoom:1.0'
10-09 16:55:00.403: D/dalvikvm(3650): GC_CONCURRENT freed 107K, 3% free 13358K/13703K, paused 2ms+14ms
10-09 16:55:00.438: D/dalvikvm(3650): GC_FOR_ALLOC freed 127K, 3% free 13626K/14023K, paused 13ms
10-09 16:55:00.573: D/dalvikvm(3650): GC_CONCURRENT freed 155K, 4% free 13963K/14407K, paused 2ms+24ms

2 个答案:

答案 0 :(得分:0)

您的文件路径存在问题。从您的路径中删除file://,如下所示:

File pdfFile = new File(Environment.getExternalStorageDirectory(),"bell.pdf");
String path = pdfFile.getAbsolutePath();
                openPdfIntent(path);

    protected void openPdfIntent(String path) {
    // TODO Auto-generated method stub
     try {
            final Intent intent = new Intent(MainActivity.this, Second.class);
            intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME,path);
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
}

答案 1 :(得分:-2)

在查看源代码并在遇到类似问题时自己玩它之后,我能够通过在调用活动时使用VIEW意图来实现这一点。此外,我必须使用该文件的某个路径,我只能从res\raw文件夹中获取它。您也可以调整它以从asset文件夹中提取,但我从未走得那么远。

// This is wherever you are launching the activity from another activity
Intent pdfIntent = new Intent(this, PdfActivity.class);
pdfIntent.setAction(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(Uri.parse("android.resource://com.your.package/raw/filename"), "application/pdf");

startActivity(pdfIntent);

当您使用VIEW操作时,该类实际上会拉取文件并将其存储到临时目录中,以便它可以正确查看它。