安卓Webview不能直接打开PDF文件,需使用第三方框架 :
Android PdfViewer:
项目地址: https://github.com/barteksc/AndroidPdfViewer
功能很强大, 使用也比较广, 亲测可以使用.
1.依赖:
compile 'es.voghdev.pdfviewpager:library:1.0.3'
2.xml布局 (可根据自己项目, 增减)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/layout"
android:id="@+id/basetop"/>
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
3.activity (第一步,下载到本地;第二步,打开。So Easy!)
public class PDFDatabaseActivity extends BaseActivity implements OnPageChangeListener , OnLoadCompleteListener {
private String mUrl = "http://web.online.jdmblock.com/币富宝超级节点计划app内部用.pdf";
@BindView(R.id.pdfView)
PDFView pdfView;
Integer pageNumber = 0;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdf);
ButterKnife.bind(this);
downFile(mUrl);
}
/**
* 文件下载
*/
private void downFile(String url) {
DownloadUtil.get().download(url,
Environment.getExternalStorageDirectory().getAbsolutePath(), "文件名",
new DownloadUtil.OnDownloadListener() {
@Override
public void onDownloadSuccess(final File file) {
runOnUiThread(new Runnable() {
public void run() {
// 下载完成打开
Uri uri = Uri.fromFile(file);
pdfView.fromUri(uri)
.defaultPage(pageNumber)
.onPageChange(PDFDatabaseActivity.this)
.enableAnnotationRendering(true)
.onLoad(PDFDatabaseActivity.this)
.spacing(0) // in dp
.load();
}
});
}
@Override
public void onDownloading(int progress) {
}
@Override
public void onDownloadFailed(Exception e) {
}
});
}
@Override
public void onPageChanged(int page, int pageCount) {
}
@Override
public void loadComplete(int nbPages) {
}
}
附:okhttp下载工具类
package com.example.btft.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* 文件下载工具类(单例模式)
*/
public class DownloadUtil {
private static DownloadUtil downloadUtil;
private final OkHttpClient okHttpClient;
public static DownloadUtil get() {
if (downloadUtil == null) {
downloadUtil = new DownloadUtil();
}
return downloadUtil;
}
public DownloadUtil() {
okHttpClient = new OkHttpClient();
}
/**
* @param url 下载连接
* @param destFileDir 下载的文件储存目录
* @param destFileName 下载文件名称,后面记得拼接后缀,否则手机没法识别文件类型
* @param listener 下载监听
*/
public void download(final String url, final String destFileDir, final String destFileName, final OnDownloadListener listener) {
Request request = new Request.Builder()
.url(url)
.build();
//异步请求
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// 下载失败监听回调
listener.onDownloadFailed(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
InputStream is = null;
byte[] buf = new byte[2048];
int len = 0;
FileOutputStream fos = null;
//储存下载文件的目录
File dir = new File(destFileDir);
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, destFileName);
try {
is = response.body().byteStream();
long total = response.body().contentLength();
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);
//下载中更新进度条
listener.onDownloading(progress);
}
fos.flush();
//下载完成
listener.onDownloadSuccess(file);
} catch (Exception e) {
listener.onDownloadFailed(e);
}finally {
try {
if (is != null) {
is.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
}
}
}
});
}
public interface OnDownloadListener{
/**
* 下载成功之后的文件
*/
void onDownloadSuccess(File file);
/**
* 下载进度
*/
void onDownloading(int progress);
/**
* 下载异常信息
*/
void onDownloadFailed(Exception e);
}
}