Android学习之浏览pdf文档

        在浏览项目的时候,发现有这么一个不错的工具【android-pdfview】,直接可以像浏览txt文档一样浏览pdf文档,这里我贴一下代码,主要是给自己标记一下。

点我下载android-pdfview工具

在布局文件中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <include layout="@layout/a1_head" />

    <com.joanzapata.pdfview.PDFView
        android:id="@+id/pdfView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#444444" />

</LinearLayout>

然后在activity中:

public class PdfViewActivity extends BaseActivity implements OnPageChangeListener{
	
	private PDFView pdfView;
	
	private int pageNum=1;
	private String filePath = Environment.getExternalStorageDirectory()
			.getPath() + "/myTest.pdf";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pdf_view);
		copyFileToSdcard();
		setActivityHeaderTitle(R.string.data_activity_name);
		getHeaderLeftButton().setImageResource(R.drawable.icon_back);
		File file=new  File(filePath);
		
		pdfView=(PDFView) findViewById(R.id.pdfView);
		pdfView.fromFile(file)
        .defaultPage(pageNum)
        .onPageChange(PdfViewActivity.this)
        .load();
		
	}

	@Override
	public void onPageChanged(int page, int pageCount) {
		page=pageNum;
	}
	private void copyFileToSdcard() {
		InputStream inputstream 	= getResources().openRawResource(
				R.raw.ceshi);
		byte[] buffer = new byte[1024];
		int count = 0;
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(new File(filePath));
			while ((count = inputstream.read(buffer)) > 0) {
				fos.write(buffer, 0, count);
			}
			fos.close();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
			Toast.makeText(PdfViewActivity.this, "Check your sdcard", Toast.LENGTH_LONG).show();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值