Android读取pdf文件及显示

来源:互联网 发布:西师大知行学院咋样 编辑:程序博客网 时间:2024/06/07 01:20
package com.tianjian.basic.activity;


import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;


import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;
import com.tianjian.util.Utils;
import com.tianjian.areaxcdoc.R;


import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 * PDF显示
 * 
 * @author wms
 * @date
 * @version
 * 
 */
public class SeePDFActivity extends ActivitySupport implements
OnPageChangeListener, OnClickListener,Handler.Callback ,OnLoadCompleteListener {
private PDFView pdfview;
private String urlString;
private Handler handler;
private File file;
private final int FILE_DOWNLOAD_SUCCESS = 0;
private final int FILE_DOWNLOAD_FAIL = 1;
private int flag=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.load_pdf_activity);
initView();
setData();
}


/**
* 初始化view
*/
private void initView() {
pdfview = (PDFView) findViewById(R.id.pdfview);
urlString = getIntent().getStringExtra("path");
title = (TextView) findViewById(R.id.title);
String str=this.getIntent().getStringExtra("title");
if(str!=null&&!str.equals("")){
title.setText(str);
}else{
title.setText("体检报告");
}
backImg = (ImageButton) findViewById(R.id.backImg);
backImg.setOnClickListener(this);
handler = new Handler(this);
}
/**
* 下载pdf
*/
private void setData() {
startProgressDialog();
Thread thread = new Thread(new Runnable() {


@Override
public void run() {

HttpURLConnection connection = null;
ByteArrayOutputStream baos = null;
BufferedOutputStream bos = null;
FileOutputStream fos = null;
try {
URL url = new URL(urlString);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
// 实现连接
connection.connect();
if (connection.getResponseCode() == 200) {
Log.e("TAG", "请求网络编号为200");
flag=2;
InputStream is = connection.getInputStream();
// 以下为下载操作
byte[] arr = new byte[1];
baos = new ByteArrayOutputStream();
bos = new BufferedOutputStream(baos);
int n = is.read(arr);
while (n > 0) {
bos.write(arr);
n = is.read(arr);
}
bos.close();
String path = Environment.getExternalStorageDirectory() + "/download/";
String[] name = urlString.split("/");
path = path + name[name.length - 1];
file = new File(path);
fos = new FileOutputStream(file);
fos.write(baos.toByteArray());
fos.close();
baos.close();
// 关闭网络连接
connection.disconnect();
handler.sendEmptyMessage(FILE_DOWNLOAD_SUCCESS);
}
} catch (Exception e) {
Log.e("TAG", "请求失败!");
Message msg = new Message();
msg.what = FILE_DOWNLOAD_FAIL;
msg.obj = "下载pdf文件失败!";
handler.sendMessage(msg );
} finally{
if(flag!=2){
Message msg = new Message();
msg.what = FILE_DOWNLOAD_FAIL;
msg.obj = "加载失败!";
handler.sendMessage(msg );
}
Log.e("TAG", "llllllllllllllllllllllllllllllll!");
if(baos != null){
try {
baos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

if(bos != null){
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

if(fos != null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

if(connection != null ){
connection.disconnect();
}
}
}
});
thread.start();


}

/**
* 加载pdf
*/
private void loadPdf() {
// 0, 2, 1, 3, 3, 3
// pdfview.fromFile(file).pages(0, 1, 1, 3, 3, 3)
// .defaultPage(1)
// .showMinimap(false)
// .enableSwipe(true)
// .onLoad(this)
// .onPageChange(SeePDFActivity.this).load();
pdfview.fromFile(file)
.onPageChange(this)
.enableAnnotationRendering(true)
.onLoad(this)
.scrollHandle(new DefaultScrollHandle(this))
.load();
}


@Override
public void onPageChanged(int page, int pageCount) {


}


@Override
public void onClick(View v) {
// TODO onClick
switch (v.getId()) {
case R.id.backImg:
finish();
break;


default:
break;
}
}


@Override
public boolean handleMessage(Message msg) {
// TODO handleMessage
switch (msg.what) {
case FILE_DOWNLOAD_SUCCESS:
loadPdf();
break;
case FILE_DOWNLOAD_FAIL:
stopProgressDialog();
String str = "";
if(msg.obj == null){
str = "操作失败!";
}else{
str = (String) msg.obj;
}
Utils.show(this, str);
finish();
break;


default:
break;
}
return false;
}


@Override
public void loadComplete(int nbPages) {
stopProgressDialog();
}


}
原创粉丝点击