Android是不能直接打开pdf文件的,需要先下载到本地再去打开
LogUtil.d(LogUtil.CQ, "donwFile.path=" + response.getAbsolutePath());
String filePath = response.getAbsolutePath();
if (response.exists()) {
if (filePath.endsWith(".pdf")) {
UIHelper.showPdfReader(mContext, filePath);//MyApplication.
} else if(filePath.endsWith(".docx") || filePath.endsWith(".doc")) {
/**
* 打开Word文件的intent
* @param param
* @return
*/
try {
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri ;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//判断版本大于等于7.0
// "com.ansen.fileprovider.fileprovider"即是在清单文件中配置的authorities
// 通过FileProvider创建一个content类型的Uri
uri = FileProvider.getUriForFile(mContext, "com.broker.liming", new File(filePath));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);// 给目标应用一个临时授权
} else {
uri = Uri.fromFile(new File(filePath));
}
intent.setDataAndType(uri, "application/msword");
mContext.startActivity(intent);
} catch (ActivityNotFoundException a){
a.getMessage();
}
} else if(filePath.endsWith(".xls") || filePath.endsWith(".xlsx")) {
/**
* 打开Excel文件的Intent
* @param param
* @return
*/
try {
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//判断版本大于等于7.0
// "com.ansen.fileprovider.fileprovider"即是在清单文件中配置的authorities
// 通过FileProvider创建一个content类型的Uri
uri = FileProvider.getUriForFile(mContext, "com.broker.liming", new File(filePath));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);// 给目标应用一个临时授权
} else {
uri = Uri.fromFile(new File(filePath));
}
// Uri uri = Uri.fromFile(new File(filePath));
intent.setDataAndType(uri, "application/vnd.ms-excel");
//application/vnd.android.package-archive
mContext.startActivity(intent);
} catch (ActivityNotFoundException a){
a.getMessage();
}
} else {
ToastUtil.showToast(mContext, "下载到本地成功");
}
} else {
LogUtil.d(LogUtil.CQ, "下载到本地的文件不存在");
}
PdfActivity
package com.broker.liming.activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import com.broker.liming.R;
import com.broker.liming.base.BaseActivity;
import com.broker.liming.utils.LogUtil;
import com.broker.liming.widget.TitleBar;
import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnDrawListener;
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
import java.io.File;
/**
* Created by qiaodong on 18年4月25日.
* 邮箱:[email protected]
* make great efforts
*/
public class PdfActivity extends BaseActivity implements OnPageChangeListener, OnLoadCompleteListener, OnDrawListener {
private PDFView pdfViewer;
private TitleBar mTitleBar;
private String filePath="/sdcard/1.pdf";
public static void actionStart(Context context,String path) {
Intent intent = new Intent(context, PdfActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path)),"application/pdf");
//如果使用Context的startActivity方法的话,就需要开启一个新的task,遇到上面那个异常的,都是因为使用了Context的startActivity方法。解决办法是,加一个flag。
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
@Override
public void initView() {
pdfViewer = (PDFView) findViewById(R.id.pdf_view);
}
@Override
protected void initTitleBar() {
mTitleBar = (TitleBar) findViewById(R.id.title_bar);
mTitleBar.setLeftBackground(R.mipmap.back);
mTitleBar.setTitle(R.string.reader_pdf);
mTitleBar.setLeftClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
@Override
public boolean initBundle(Bundle bundle) {
Intent intent = getIntent();
if (intent != null) {
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
filePath = Uri.decode(intent.getData().getEncodedPath());
LogUtil.d(LogUtil.CQ,"filePath="+filePath);
}
}
return super.initBundle(bundle);
}
@Override
protected int getContentView() {
return R.layout.activity_pdf;
}
@Override
protected void onResume() {
super.onResume();
show();
// pdfViewer.onResume(true);
}
@Override
protected void onPause() {
super.onPause();
// pdfViewer.onPause();
}
private void show() {
LogUtil.d(LogUtil.CQ,"showfilePath="+filePath);
pdfViewer.fromFile(new File(filePath))
.defaultPage(0)//默认加载页数
.enableSwipe(true)//是否允许翻页,默认是允许翻
.swipeHorizontal(false)// true:水平翻页 false:竖直翻页
.enableDoubletap(true)//双击
.onPageChange(this)
.onLoad(this)
// .pages(2, 5) // 加载显示的页数,默认加载所有的页数
.onDraw(this)//绘图监听,可选的,并允许你画的东西提供的画布上,在当前页面上方
// .onPageScroll(onPageScrollListener)
// .onError(onErrorListener)
// .enableAntialiasing(true) //低分辨率渲染
// .spacing(0)//页与页之间的距离
.enableAnnotationRendering(true)// render annotations (such as comments, colors or forms)渲染注释
.password(null)//密码
.scrollHandle(null)//滚动处理
.load();
}
/**
* 翻页回调
*
* @param page
* @param pageCount
*/
@Override
public void onPageChanged(int page, int pageCount) {
}
/**
* 加载完成回调
* @param nbPages 总共的页数
*/
@Override
public void loadComplete(int nbPages) {
}
@Override
public void onLayerDrawn(Canvas canvas, float pageWidth, float pageHeight, int displayedPage) {
}
}