• 【flutter上传图片】


    1.使用multi_image_picker插件

    //选择图片
    openPhotoSelect(int maxImages,context) async {
        try {
          List<Asset> images = await MultiImagePicker.pickImages(
            maxImages: maxImages - state.selectImageList.length,
            enableCamera: true,
            cupertinoOptions: CupertinoOptions(takePhotoIcon: "chat"),
            materialOptions: MaterialOptions(
              actionBarTitle: "图片选择",
              allViewTitle: "所有图片",
              useDetailsView: true,
              selectCircleStrokeColor: "#4dc8b6",
              selectionLimitReachedText: "最多选择$maxImages张图片",
            ),
          );
    
          state.selectImageList.addAll(file);
          state.publishType = 1;
          update();
        } on Exception catch (e) {
          print(e);
        }
      }
    
    		//选择的图片显示
    		AssetThumb(
                asset: asset,
                width: 105,
                height: 105,
              )
    
    //上传图片
      Future<bool> uploadImagesData() async {
        state.imageVideoParamList.clear();
        ByteData byteData;
        List<int> imageData;
        MultipartFile multipartFile;
        FormData formData;
        Response response;
        Dio dio = Dio();
    
        if(state.selectImageList.length > 0){
          for (int i = 0; i < state.selectImageList.length; i++) {
            Asset asset = state.selectImageList[i];
            byteData = await asset.getByteData();
            imageData = byteData.buffer.asUint8List();
            multipartFile =
                MultipartFile.fromBytes(imageData, filename: "${asset.name}");
            formData = FormData.fromMap({
              "android": "1",
              "file": multipartFile,
            });
    
            response = await dio.post('${MyApi.UPLOAD_FILE}', data: formData);
            if (response.statusCode == 200 && response.data['code'] == 200) {
              state.uploadIndex = i + 1;
              if (i == 0) {
                state.image = response.data['data'] /*+
                    '?Size=${asset.originalWidth}x${asset.originalHeight}'*/;
    
                state.imageVideoParamList.add(
                    {
                      'type':'P',
                      'url':'${response.data['data'] +
                          '?Size=${asset.originalWidth}x${asset.originalHeight}'}'
                    }
                );
    
                update();
              } else {
                state.image = "${state.image};" +
                    response.data['data'] +
                    '?Size=${asset.originalWidth}x${asset.originalHeight}';
                state.imageVideoParamList.add(
                    {
                      'type':'P',
                      'url':'${response.data['data'] +
                          '?Size=${asset.originalWidth}x${asset.originalHeight}'}'
                    });
                update();
              }
            }
          }
          // 图片上传结束
          if (state.selectImageList.length > state.uploadIndex) {
            // 部分图片上传失败
            state.uploadIndex = null;
            return false;
          }
        }
        return true;
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92

    2.使用wechat_assets_picker插件

    // 打开图片选择
      openPhotoSelect(int maxImages,context) async {
        try {
          List<AssetEntity> images = await AssetPicker.pickAssets(
              context,
              maxAssets: maxImages - state.selectImageList.length
          );
          images.forEach((element) async {
            File file = await element.file;
            state.selectImageList.add(file);
          });
          state.publishType = 1;
          update();
        } on Exception catch (e) {
          print(e);
        }
      }
    //选择的图片的显示(加透明度渐变动画)
    Image(
                width: 105,
                height: 105,
                cacheWidth: 105,
                cacheHeight: 105,
                image: AssetEntityImageProvider(assetEntity, isOriginal: false),
                fit: BoxFit.fill,
                frameBuilder: (context, child, frame, wasSynchronouslyLoaded){
                  if(wasSynchronouslyLoaded){
                    return child;
                  }
                  return AnimatedOpacity(
                    child: child,
                    opacity: frame == null ? 0 : 1,
                    duration: const Duration(seconds: 4),
                    curve: Curves.easeOut,
                  );
                }
              )
    //选择的图片的显示(wechat_assets_picker插件提供的方法)
    /*Image(
                width: 105,
                height: 105,
                image: AssetEntityImageProvider(assetEntity, isOriginal: false),
                fit: BoxFit.fill,*/
    
    //上传图片
      Future<bool> uploadImagesData() async {
        state.imageVideoParamList.clear();
        ByteData byteData;
        List<int> imageData;
        MultipartFile multipartFile;
        FormData formData;
        Response response;
        Dio dio = Dio();
    
        if(state.selectImageList.length > 0){
          for (int i = 0; i < state.selectImageList.length; i++) {
            File file = state.selectImageList[i];
            String imageName = file.path.substring(file.path.lastIndexOf("/") + 1, file.path.length);
            multipartFile =
                MultipartFile.fromFileSync(file.path, filename: "$imageName");
            formData = FormData.fromMap({
              "android": "1",
              "file": multipartFile,
            });
    
            response = await dio.post('${MyApi.UPLOAD_FILE}', data: formData);
            if (response.statusCode == 200 && response.data['code'] == 200) {
              state.uploadIndex = i + 1;
              if (i == 0) {
                state.image = response.data['data'];
    
                state.imageVideoParamList.add(
                    {
                      'type':'P',
                      'url':'${response.data['data']}'
                    }
                );
    
                update();
              } else {
                state.image = "${state.image};" +
                    response.data['data'];
    
                state.imageVideoParamList.add(
                    {
                      'type':'P',
                      'url':'${response.data['data']}'
                    });
                update();
              }
            }
          }
          // 图片上传结束
          if (state.selectImageList.length > state.uploadIndex) {
            // 部分图片上传失败
            state.uploadIndex = null;
            return false;
          }
        }
    
        return true;
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102

    选择视频

    /// 视频选择
      void openVideoSelect(BuildContext context) async {
        PickedFile pickedFile =
        await ImagePicker().getVideo(source: ImageSource.gallery);
        File videoFile = File(pickedFile.path);
        //state.videoPath = videoFile.path;
        update();
        VideoPlayerController _controller = VideoPlayerController.file(videoFile);
        _controller.initialize().then((_) async {
          Duration duration = _controller.value.duration;
          if (duration.inSeconds > 60) {
            esLoadingToast('发布视频长度不能大于1分钟');
          } else {
            // 解码视频
            Loading.showLoading(context);
           bool end = await openVideoEncode(videoFile.path);
           if(end){
             Loading.hideLoading(context);
           }
          }
        });
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    【JAVA】普通IO数据拷贝次数的问题探讨
    产品经理基础--02需求收集&需求管理
    (02)Cartographer源码无死角解析-(28) GlobalTrajectoryBuilder构建过程与整体分析
    Netty
    SpringCloudGateway--自动路由映射
    最详细说明spring cloud和Spring Cloud Alibaba的联系和区别
    lspci详解
    Cholesterol-PEG-DBCO,CLS-PEG-DBCO,胆固醇-聚乙二醇-二苯基环辛炔PEG衍生物
    运算符重载的三种实现方法
    mysql日志总结
  • 原文地址:https://blog.csdn.net/androidhyf/article/details/132673474