10

我想在我正在做的应用程序中实现一个 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();
        }
    }

}

二等舱

package android.pdf.reader;

import net.sf.andpdf.pdfviewer.PdfViewerActivity;
import android.os.Bundle;

public class Second extends PdfViewerActivity 
{

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    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;
}
}
4

3 回答 3

26

使用下面的代码。

第一个.java

public class First extends ListActivity {

    String[] pdflist;
    File[] imagelist;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);

        File images = Environment.getExternalStorageDirectory();
        imagelist = images.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return ((name.endsWith(".pdf")));
            }
        });
        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);
        String path = 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();
        }
    }
}

二.java

public class Second extends PdfViewerActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        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;
    }
}

并将这两个活动声明到您的清单文件中。

于 2012-06-22T09:29:38.800 回答
3

在 SO 上有一篇很好的帖子。特别是查看 Commons.Ware 给出的答案,它回答了您的问题。

根据您的评论,我在上面提到的 SO 帖子中添加了链接(您找不到的项目的来源):

因此,“签出”或将存储库克隆到本地文件系统以浏览代码。正如我在评论中提到的那样,在您进一步查看您是否可以和不能对代码做什么之前,请检查每个库的许可证。

于 2012-06-22T09:05:16.950 回答
3

这个对我有用。

1) 将 PdfViewer.jar 添加到项目的构建路径中

2) 将以下可绘制资源从 PdfViewer/res/drawable 复制到 YourProject/res/drawable left_arrow.png right_arrow.png zoom_in.png zoom_out.png

3) 将以下布局资源从PdfViewer/res/layout 复制到YourProject/res/layout dialog_pagenumber.xml pdf_file_password.xml

4) 从 net.sf.andpdf.pdfviewer.PdfViewerActivity 导出您的 PDF 活动

5)使用默认的drawables和布局:

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; }
enter code here

6) 使用以下代码调用派生的 PdfViewActivity:

Intent intent = new Intent(this, YourPdfViewerActivity.class);

intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, "PATH TO PDF GOES HERE");

startActivity(intent);

您可以从此链接下载源代码。关联

希望这可以帮助 :)

于 2014-11-12T12:01:24.783 回答