Android下载pdf并在本地预览

废话不多,直接上代码!!!!!

布局代码如下:

<?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();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

只懂写HelloWorld

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值