由于项目开发需要,需要了一个查看在线PDF的功能,写下这个测试Demo,供以后查阅方便。
1.新建一个Android项目
2.添加依赖类 compile 'es.voghdev.pdfviewpager:library:1.0.3'
3.实现类代码
public classMainActivityextendsAppCompatActivityimplementsDownloadFile.Listener {
privateRemotePDFViewPagerpdfViewPager;
privateEditTexteditText;
privateButtonbutton;
privatePDFPagerAdapteradapter;
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
setDownloadButtonListener();
}
private voidsetDownloadButtonListener() {
finalContext ctx =this;
finalDownloadFile.Listener listener =this;
button.setOnClickListener(newView.OnClickListener() {
@Override
public voidonClick(View v) {
pdfViewPager=newRemotePDFViewPager(ctx,editText.getText().toString().trim(),listener);
pdfViewPager.setId(R.id.pdf_view);
hideDownloadButton();
}
});
}
private voidinitUI() {
editText= (EditText) findViewById(R.id.urlEdit);
button= (Button) findViewById(R.id.openUrlBtn);
}
@Override
public voidonSuccess(String url,String destinationPath) {
adapter=newPDFPagerAdapter(this,FileUtil.extractFileNameFromURL(url));
pdfViewPager.setAdapter(adapter);
showDownloadButton();
setContentView(pdfViewPager);
}
@Override
public voidonFailure(Exception e) {
e.printStackTrace();
showDownloadButton();
Toast.makeText(this,"下载失败",Toast.LENGTH_SHORT).show();
}
@Override
public voidonProgressUpdate(intprogress, inttotal) {
}
public voidshowDownloadButton() {
button.setVisibility(View.VISIBLE);
}
public voidhideDownloadButton() {
button.setVisibility(View.INVISIBLE);
}
@Override
protected voidonDestroy() {
super.onDestroy();
if(adapter!=null) {
adapter.close();
}
}
}
4.添加权限 网络、读取存储、写存储的权限
5.当然还有其他是使用方法,具体参考地址:https://github.com/voghDev/PdfViewPager