In Flutter How to get image path after selecting images using Multi Image Picker?

后端 未结 4 1401
执笔经年
执笔经年 2021-01-18 18:13

I want to get an image path from selected multiple images, I\'m using this link to select multiple images but I got assets, I want paths from selected multiple images becaus

4条回答
  •  盖世英雄少女心
    2021-01-18 18:43

    Use multi_image_picker 4.7.14 library to pick Multiple Images. use below code you can send selected asset image as a file. `

    //Inside Widget Builder
    FlatButton(
                child: Image.asset(
                  "assets/images/camera.png",
                  color: Colors.grey,
                ),
                onPressed: loadAssets,
              ),
              
        SizedBox(
                      // height: SizeConfig.safeBlockHorizontal * 10,
                      height: MediaQuery.of(context).size.height / 2,
                      child: Column(
                        children: [
                          Expanded(
                            child: buildGridView(),
                          ),
                        ],
                      ),
                    )

    List fileImageArray = [];
      List f = List();
      List resultList = List();
      List images = List();
      
      Future loadAssets() async {
        try {
          resultList = await MultiImagePicker.pickImages(
            maxImages: 4,
            enableCamera: true,
            selectedAssets: images,
            cupertinoOptions: CupertinoOptions(takePhotoIcon: "chat"),
            materialOptions: MaterialOptions(
              actionBarColor: "#abcdef",
              actionBarTitle: "Example App",
              allViewTitle: "All Photos",
              useDetailsView: false,
              selectCircleStrokeColor: "#000000",
            ),
          );
        } on Exception catch (e) {
          error = e.toString();
        }
    
        if (!mounted) return;
    
        for (int i = 0; i < resultList.length; i++) {
          var path =
              await FlutterAbsolutePath.getAbsolutePath(resultList[i].identifier);
          print(path);
          f.add(File(path));
        }
    
        setState(() {
          images = resultList;
        });
    
        // return fileImageArray;
      }
    
    //image PreView
      Widget buildGridView() {
        return GridView.count(
          crossAxisCount: 4,
          children: List.generate(images.length, (index) {
            Asset asset = images[index];
            return AssetThumb(
              asset: asset,
              width: 50,
              height: 50,
            );
          }),
        );
      }

    API Call : while image uploading place use multipart file

    List newList = new List();
        
      Future multiImagePostAPI() async {
        var request = http.MultipartRequest('POST', url);
        request.headers["Authorization"] = pref.getString("token");
        request.headers["Accept"] = "application/json";
        
         for (int i = 0; i < f.length; i++) {
          newList.add(await http.MultipartFile.fromPath('photo[]', f[i].path));
        }
        request.files.addAll(newList);
         Map data = Map();
          data["user_id"] = user_id;
          data["project_id"] = pro_id;
          data["title"] = titleController.text;
    
          request.fields.addAll(data);
    
          var res = await request.send();
         
         if (res.statusCode == 200) {
          debugPrint("Status$res");
         }else {
          debugPrint("status : $res");
          }
          
         
         }

提交回复
热议问题