废话不多,直接上代码!!!!!
布局代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
获取对应网址上的pdf代码如下:
//下载pdf
public void getPDF() {
final String outFilePath = Environment.getExternalStorageDirectory().getPath() + "/hello/" + ".pdf";
HttpOkhUtils.getInstance().download(url_pdf+task_id,
new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//下载功能
InputStream inputStream = response.body().byteStream();
FileOutputStream outputStream = new FileOutputStream(new File(outFilePath));
byte[] by = new byte[2048];
int len = 0;
while ((len = inputStream.read(by)) != -1) {
outputStream.write(by, 0, len);
}
outputStream.flush();
}
});
}
activity代码:
package com.tencent.newmonkey.app.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import com.github.barteksc.pdfviewer.PDFView;
import com.tencent.newmonkey.newmonkeymobilewithnoroot.R;
import java.io.File;
/**
* @author llh
* **/
//pdf显示界面
public class PDFActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdf);
PDFView pdfView = findViewById(R.id.pdfView);
//页面数据传输
Intent intent = getIntent();
String task_id = intent.getStringExtra("task_id");
//pdf View 加载本地路径的pdf文件
pdfView.fromFile(new File(Environment.getExternalStorageDirectory().getPath() + "/hello/" + ".pdf"))
.defaultPage(0)
.enableAnnotationRendering(true)
.scrollHandle(null)
.load();
pdfView.resetZoom();
}
}