最近的项目中需要嵌入一个打开pdf下载查看的功能,研究了一下,这里写出来做一个备忘,顺便可以和大家做一下交流学习。好了,首先我们先考虑一下这个需求:
1.网络下载
2.下载后的保存文件地址
3.通过文件地址展示pdf
没错,就是这么简单:
下面附上代码demo
part1:mainactivity,
这里我先是使用了打开本地的assets文件
public class MainActivity extends AppCompatActivity {
private Context mContext=this;
private Button button;
private Handler handler=new Handler();
private PDFView pdfView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdf);
button= (Button) findViewById(R.id.button);
pdfView = (PDFView) findViewById(R.id.pdfView);
// 在我这个测试例子中,事先准备一个叫做sample.pdf的pdf大文件放到assets目录下。
// 从assets文件目录下读取名为 sample.pdf的文件,缺省把该pdf定位到第一页。
pdfView.fromAsset("sample.pdf").defaultPage(1).onPageChange(new OnPageChangeListener() {
@Override
public void onPageChanged(int page, int pageCount) {
// 当用户在翻页时候将回调。
Toast.makeText(getApplicationContext(), page + " / " + pageCount, Toast.LENGTH_SHORT).show();
}
}).load();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(mContext,DownLoaderActivity.class);
startActivity(intent);
}
});
}
}
part2:这里是我在点击按钮之后进入的下载并展示页面
public class DownLoaderActivity extends AppCompatActivity
implements OnPageChangeListener,OnLoadCompleteListener,OnDrawListener {
private OkHttpClient okHttpClient;
private ProgressBar progressBar;
private String url= "https://pic.bincrea.c"+
"om/bc_bg_6D40C91A170D41C182511ABBB8A634A4.pdf";
private TextView textView;
private PDFView pdfView;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
//进度条的值
int i = msg.arg1;
progressBar.setProgress(i);
}
if (msg.arg1==100){
displayFromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath(), url.substring(url.lastIndexOf("/") + 1)));
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_down_loader);
textView = (TextView) findViewById(R.id.textView);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
pdfView = (PDFView) findViewById(R.id.pdfView);
okHttpClient = new OkHttpClient(
Request request = new Request.Builder().url(url).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d("h_bl", "onFailure");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
InputStream is = null;
byte[] buf = new byte[2048];
int len = 0;
FileOutputStream fos = null;
String SDPath = Environment.getExternalStorageDirectory().getAbsolutePath();
try {
is = response.body().byteStream();
long total = response.body().contentLength();
File file = new File(SDPath, url.substring(url.lastIndexOf("/") + 1));
fos = new FileOutputStream(file);
long sum = 0;
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
sum += len;
int progress = (int) (sum * 1.0f / total * 100);
Log.d("h_bl", "progress=" + progress);
Message msg = handler.obtainMessage();
msg.what = 1;
msg.arg1 = progress;
handler.sendMessage(msg);
}
fos.flush();
Log.d("h_bl", "文件下载成功");
} catch (Exception e) {
} finally {
try {
if (is != null)
is.close();
} catch (IOException e) {
}
try {
if (fos != null)
fos.close();
} catch (IOException e) {
}
}
}
});
}
private void displayFromFile( File file ) {
pdfView.fromFile(file) //设置pdf文件地址
.defaultPage(1) //设置默认显示第1页
.onPageChange(this) //设置翻页监听
.onLoad(this) //设置加载监听
.onDraw(this) //绘图监听
.showMinimap(false) //pdf放大的时候,是否在屏幕的右上角生成小地图
.swipeVertical( false ) //pdf文档翻页是否是垂直翻页,默认是左右滑动翻页
.enableSwipe(true) //是否允许翻页,默认是允许翻
// .pages( 2 ,5 ) //把2 5 过滤掉
.load();
}
@Override
public void onPageChanged(int page, int pageCount) {
Toast.makeText( DownLoaderActivity.this , " " + page +
" / " + pageCount , Toast.LENGTH_SHORT).show();
}
/**
* 加载完成回调
* @param nbPages 总共的页数
*/
@Override
public void loadComplete(int nbPages) {
Toast.makeText( DownLoaderActivity.this , "加载完成" + nbPages , Toast.LENGTH_SHORT).show();
}
@Override
public void onLayerDrawn(Canvas canvas, float pageWidth, float pageHeight, int displayedPage) {
// Toast.makeText( MainActivity.this , "pageWidth= " + pageWidth + "
// pageHeight= " + pageHeight + " displayedPage=" + displayedPage , Toast.LENGTH_SHORT).show();
}
}
具体的布局文件我就不贴了,过于简单,相信你可以1秒钟写完,haha