实现PDF阅读器的代码示例

我想在正在执行的应用程序中实现PDF阅读器,我发现了几个API,但是它们都不是开源的。


你们中有人知道免费的替代品吗?


OP 对Dipak Keshariya解决方案的略微调整

头等舱


package android.pdf.reader;


import java.io.File;

import java.io.FilenameFilter;


import net.sf.andpdf.pdfviewer.PdfViewerActivity;


import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.os.Environment;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.ListView;


public class First extends Activity {


    @Override

    public void onCreate(Bundle savedInstanceState) 

    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);


        File images = Environment.getExternalStorageDirectory();

        File[] imagelist = images.listFiles(new FilenameFilter()

        {  

                public boolean accept(File dir, String name)  

                {  

                        return ((name.endsWith(".pdf")));

                }  

        }); 

        String[] pdflist = new String[imagelist.length]; 

        for(int i = 0;i<imagelist.length;i++)

        {

                pdflist[i] = imagelist[i].getName();

        }

        this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, pdflist));

    }


    protected void onListItemClick(ListView l, View v, int position, long id) 

    {

            super.onListItemClick(l, v, position, id);

            Object[] imagelist;

            String path = ((File) imagelist[(int)id]).getAbsolutePath();

            openPdfIntent(path);

    }


    private void openPdfIntent(String path) 

    {

        try

        {

          final Intent intent = new Intent(First.this, Second.class);

          intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);

          startActivity(intent);

        }

        catch (Exception e) 

        {

          e.printStackTrace();

        }

    }


}

四季花海
浏览 491回答 2
2回答

慕容708150

为此使用下面的代码。First.javapublic class First extends ListActivity {&nbsp; &nbsp; String[] pdflist;&nbsp; &nbsp; File[] imagelist;&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; //setContentView(R.layout.main);&nbsp; &nbsp; &nbsp; &nbsp; File images = Environment.getExternalStorageDirectory();&nbsp; &nbsp; &nbsp; &nbsp; imagelist = images.listFiles(new FilenameFilter() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public boolean accept(File dir, String name) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ((name.endsWith(".pdf")));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; pdflist = new String[imagelist.length];&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < imagelist.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pdflist[i] = imagelist[i].getName();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; this.setListAdapter(new ArrayAdapter<String>(this,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android.R.layout.simple_list_item_1, pdflist));&nbsp; &nbsp; }&nbsp; &nbsp; protected void onListItemClick(ListView l, View v, int position, long id) {&nbsp; &nbsp; &nbsp; &nbsp; super.onListItemClick(l, v, position, id);&nbsp; &nbsp; &nbsp; &nbsp; String path = imagelist[(int) id].getAbsolutePath();&nbsp; &nbsp; &nbsp; &nbsp; openPdfIntent(path);&nbsp; &nbsp; }&nbsp; &nbsp; private void openPdfIntent(String path) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final Intent intent = new Intent(First.this, Second.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}Second.javapublic class Second extends PdfViewerActivity {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated method stub&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; }&nbsp; &nbsp; public int getPreviousPageImageResource() {&nbsp; &nbsp; &nbsp; &nbsp; return R.drawable.left_arrow;&nbsp; &nbsp; }&nbsp; &nbsp; public int getNextPageImageResource() {&nbsp; &nbsp; &nbsp; &nbsp; return R.drawable.right_arrow;&nbsp; &nbsp; }&nbsp; &nbsp; public int getZoomInImageResource() {&nbsp; &nbsp; &nbsp; &nbsp; return R.drawable.zoom_in;&nbsp; &nbsp; }&nbsp; &nbsp; public int getZoomOutImageResource() {&nbsp; &nbsp; &nbsp; &nbsp; return R.drawable.zoom_out;&nbsp; &nbsp; }&nbsp; &nbsp; public int getPdfPasswordLayoutResource() {&nbsp; &nbsp; &nbsp; &nbsp; return R.layout.pdf_file_password;&nbsp; &nbsp; }&nbsp; &nbsp; public int getPdfPageNumberResource() {&nbsp; &nbsp; &nbsp; &nbsp; return R.layout.dialog_pagenumber;&nbsp; &nbsp; }&nbsp; &nbsp; public int getPdfPasswordEditField() {&nbsp; &nbsp; &nbsp; &nbsp; return R.id.etPassword;&nbsp; &nbsp; }&nbsp; &nbsp; public int getPdfPasswordOkButton() {&nbsp; &nbsp; &nbsp; &nbsp; return R.id.btOK;&nbsp; &nbsp; }&nbsp; &nbsp; public int getPdfPasswordExitButton() {&nbsp; &nbsp; &nbsp; &nbsp; return R.id.btExit;&nbsp; &nbsp; }&nbsp; &nbsp; public int getPdfPageNumberEditField() {&nbsp; &nbsp; &nbsp; &nbsp; return R.id.pagenum_edit;&nbsp; &nbsp; }}并将这两个活动声明到清单文件中。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android