<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include layout="@layout/common_title" /> <com.github.barteksc.pdfviewer.PDFView android:id="@+id/pdf_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
if (CheckFileExist(title)){
builderShow = new CustomDialog(ShowPDFActivity.this);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dialog_pdf_progress_new, null);
builderShow.setContentView(view);
builderShow.show();
isDownload=false;
refushUI();
}else {
isDownload=true;
DownLoadPDF.getInstance().downLoadPDF(ShowPDFActivity.this, //下载路径);
}
public void refushUI(){
try {
pdfView.fromFile(new File(//pdf文件的绝对路径,//标题))
.defaultPage(1)
.enableAnnotationRendering(false)
.onLoad(new OnLoadCompleteListener() {
@Override
public void loadComplete(int nbPages) {
if (isDownload){
DownLoadPDF.getInstance().closeDilaoig();
}
if (builderShow != null&&builderShow.isShowing()) {
builderShow.dismiss();
}
}
})
.scrollHandle(null)
.load();
}catch (Exception e){
e.printStackTrace();
}
}
private void displayFromAssets(String assetFileName ) {
pdfView.fromAsset(assetFileName) //设置pdf文件地址
.defaultPage(6) //设置默认显示第1页
.onPageChange(this) //设置翻页监听
.onLoad(this) //设置加载监听
.onDraw(this) //绘图监听
.showMinimap(false) //pdf放大的时候,是否在屏幕的右上角生成小地图
.swipeVertical( false ) //pdf文档翻页是否是垂直翻页,默认是左右滑动翻页
.enableSwipe(true) //是否允许翻页,默认是允许翻页
// .pages( 2 , 3 , 4 , 5 ) //把2 , 3 , 4 , 5 过滤掉
.load();
}
private void displayFromFile( File file ) {
pdfView.fromFile(file) //设置pdf文件地址
.defaultPage(6) //设置默认显示第1页
.onPageChange(this) //设置翻页监听
.onLoad(this) //设置加载监听
.onDraw(this) //绘图监听
.showMinimap(false) //pdf放大的时候,是否在屏幕的右上角生成小地图
.swipeVertical( false ) //pdf文档翻页是否是垂直翻页,默认是左右滑动翻页
.enableSwipe(true) //是否允许翻页,默认是允许翻
// .pages( 2 , 3 , 4 , 5 ) //把2 , 3 , 4 , 5 过滤掉
.load();
}
public class DownLoadPDF {
private static Context context;
private static File file ;
private static CustomDialog builder = null ;
private static Handler ddhandle;
private static DownLoadPDF instance = null;
public static DownLoadPDF getInstance(){
if(instance==null){
synchronized (DownLoadPDF.class){
if(instance==null){
instance = new DownLoadPDF();
}
}
}
return instance;
}
public void downLoadPDF(final Context con, final String url, final String title, final Handler ddhandler) {
ddhandle = ddhandler;
context = con;
builder = new CustomDialog(con);
LayoutInflater inflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dialog_pdf_progress_new, null);
builder.setContentView(view);
builder.show();
new Thread() {
@Override
public void run() {
try {
file = getFileFromServer(url,title);
sleep(200);
if (file != null) {
handler.sendEmptyMessage(2);
}
} catch (Exception e) {
e.printStackTrace();
builder.dismiss();
handler.sendEmptyMessage(-1);
}
}
}.start();
}
public void closeDilaoig(){
if (builder != null&&builder.isShowing()) {
builder.dismiss();
}
}public static int length ;
public static File getFileFromServer(String path,String title)
throws Exception {
// 如果相等的话表示当前的sdcard挂载在手机上并且是可用的
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setDoInput(true);
conn.connect();
length = conn.getContentLength();
InputStream is = conn.getInputStream();
//将pdf文件存储在指定文件夹下
File filePath = new File(//指定文件夹路径);
if (!filePath.exists()){
filePath.mkdir();
}
File file = new File(filePath , title+".pdf");
FileOutputStream fos = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buffer = new byte[1024];
int len;
while ((len = bis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
handler.sendEmptyMessage(0);
}
fos.close();
bis.close();
is.close();
return file;
} else {
handler.sendEmptyMessage(-1);
return null;
}
}
private static Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0:
break;
case -1:
//下载失败
Toast.makeText(context, "下载失败,请稍后再试!", Toast.LENGTH_SHORT).show();
break;
case 2:
ddhandle.sendEmptyMessage(100);
break;
default:
break;
}
}
};
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有