zoukankan      html  css  js  c++  java
  • flutter 下载文件

    android 

      1.在 android/app/app/src/main/AndroidManifest.xml 中添加读取文件的权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

      2. 下载方法

    Future downloadFile({String url}) async {
      Dio dio = new Dio();
      String path = await getLocalStorage('serverPath');
      dio.options.baseUrl = path == null ? "http://192.168.1.170:8180/" : path;
      //设置连接超时时间
      dio.options.connectTimeout = 10000;
      //设置数据接收超时时间
      dio.options.receiveTimeout = 10000;
      Response response;
      try {
        response = await dio.download(url, "/storage/emulated/0/test.png");
        if (response.statusCode == 200) {
          print('下载请求成功');
        } else {
          throw Exception('接口出错');
        }
      } catch (e) {
        showTotast('服务器出错或网络连接失败!');
        return print('ERROR:======>$e');
      }
    }

      3.  文件权限不仅需要需要在AndroidManifest中配置,还需要在使用前check是否真正拥有权限,以动态申请,否则会报错的

         申请权限使用 permission_handler 组件,5.0版本 之前的写法跟之后的写法不一样

     permission_handler: ^5.0.0 # 检测权限,没有权限就申请
     bool status = await Permission.storage.isGranted;
        //判断如果还没拥有读写权限就申请获取权限
        if (!status) {
          return await Permission.storage.request().isGranted;
        } 
    // 调用下载方法 --------做该做的事

  • 相关阅读:
    网页导出PDF文件
    图片翻转导航
    瀑布流之ajax
    楼梯效果
    数码时钟
    数字字母随机验证码
    kafka shell
    zookeeper shell
    正则
    Linux(一)
  • 原文地址:https://www.cnblogs.com/bruce-gou/p/13810040.html
Copyright © 2011-2022 走看看