• okhttp下载文件并直接显示到手机上


    最近天气有点冷,吹牛逼之前,先热个身O(∩_∩)O哈哈~,

     

    整理了之前写的方法,重新修改了文件下载demo,新demo从服务器下载文件地址(以图片和pdf文件为例):稍后附上

    https://download.csdn.net/download/qq_35702985/11688476

    如果调用okhttp工具类下载服务器文件报错

    请按以下步骤检查:

    1.动态申请权限,同时AndroidManifest.xml的application中添加

    android:usesCleartextTraffic="true"

    2.下载文件函数(downFile)在线程中调用

    3.downFile的第二个参数文件路径写成相对路径(Environment.getExternalStorageDirectory() + "/download/")

     

    相关代码:

    public void checkPemission() {
    Log.e("TAG", "进入了checkPemission()");
    int hasWritePermission = PermissionChecker.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (hasWritePermission != PermissionChecker.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 9);
    } else {
    new Thread() {
    @Override
    public void run() {
    super.run();
    downFile(urlString);
    }
    }.start();
    }
    }

    /**
    * 文件下载
    */
    private void downFile(String url) {
    DownloadUtil.get().download(url, Environment.getExternalStorageDirectory() + "/pdf/", "test.pdf",
    new DownloadUtil.OnDownloadListener() {
    @Override
    public void onDownloadSuccess(File file) {

    openPDF(file);//打开PDF文件
    }

    @Override
    public void onDownloading(int progress) {

    }

    @Override
    public void onDownloadFailed(Exception e) {
    //下载异常进行相关提示操作

    }
    });
    }


    源码地址: 稍后附上 https://download.csdn.net/download/qq_35702985/11643721

  • 相关阅读:
    How To Run Docker in Docker Container [3 Easy Methods]
    design patterns of refactoring guru
    MathJax A JavaScript display engine for mathematics that works in all browsers.
    SoC the root design principle
    Kubernetes plugin for Jenkins
    Inversion of Control
    Python Metaclasses
    JNLP the foundametal of distributed computing of Jenkins
    C# 表达式树Expression
    ML .NET 电影评论情绪分析
  • 原文地址:https://www.cnblogs.com/dmrbell/p/11434260.html
Copyright © 2020-2023  润新知