Android 下载网络pdf到本地 并加载

Android 加载pdf文件目前只支持 本地pdf加载的方式 
无法像IOS原生web展示pdf,所以要
1.先下载到本地,2.再去加载本地pdf文件
加载方式有多种,本次列取了pdf-viewer的方式,其他方式还有微信X5内核 ,android_pdf ,SuperWeb,Moliza开源的Pdf.js等 各有利弊

  • 下载网络pdf文件到本地  
  •  String outFilePath = DOWN_LOAD_PATH + System.currentTimeMillis() + ".pdf";
    HttpOkhUtils.getInstance().download(“网络图片地址”,
                    new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            WaitDialog.dismiss();
    
                        }
    
                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            WaitDialog.dismiss();
                            //下载功能
                            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();
    //                        ToastUtils.showToast("下载成功");
    
                            //pdf View 加载本地路径的pdf文件
                            pdfView.fromFile(new File(outFilePath))
                                    .defaultPage(0)
                                    .enableAnnotationRendering(true)
                                    .scrollHandle(null)
                                    .load();
                            pdfView.resetZoom();
    
    
                        }
                    });
    /**
     * @author: FX
     * Eamail: 876111689@qq.com
     * Description: HttpLog
     */
    
    public class HttpLog implements HttpLoggingInterceptor.Logger {
        @Override
        public void log(String message) {
            Log.d("HttpLogInfo", message);
        }
    }

     

  • 请求工具类
     

       implementation 'com.squareup.okhttp3:okhttp:3.12.3'
       implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'
    public class HttpOkhUtils {
        // 网络请求超时时间值(s)
        private static final int          DEFAULT_TIMEOUT = 30;
        private static       HttpOkhUtils okhUtils;
        private              OkHttpClient client;
    
        private HttpOkhUtils() {
            Interceptor logInterceptor = new HttpLoggingInterceptor(new HttpLog()).setLevel(HttpLoggingInterceptor.Level.BODY);
            client = new OkHttpClient.Builder()
                    .readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                    .addInterceptor(logInterceptor)
                    .build();
        }
    
        public static HttpOkhUtils getInstance() {
            if (okhUtils == null) {
                synchronized (HttpOkhUtils.class) {
                    if (okhUtils == null)
                        okhUtils = new HttpOkhUtils();
                }
            }
            return okhUtils;
        }
      public void download(String url,Callback callback){
            Request request=new Request.Builder().url(url).get().build();
            Call call=client.newCall(request);
            call.enqueue(callback);
        }
    }

    PDF :
     

     //PDF 查看
        implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
    
        <com.github.barteksc.pdfviewer.PDFView
            android:id="@+id/pdfView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
           />

     

  • PDF加载
     

      pdfView.fromFile(new File(outFilePath))
                                    //默认加载第0页
                                    .defaultPage(0)
                                    //支持印章等 格式
                                    .enableAnnotationRendering(true)
                                    .scrollHandle(null)
                                    .load();
                            pdfView.resetZoom();

    需要的同学可以复制到自己的项目中,以上为全部 所需代码
     

评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值