how to rename a pickedfile from image_picker in flutter?

1,117

PickedFile is not File, it's just a result object of picked file.

You can use it's path to create a File and use File's renameSync:

File(_imageFile.path).renameSync(newPath);
Share:
1,117
Technical Support Team
Author by

Technical Support Team

Updated on December 26, 2022

Comments

  • Technical Support Team
    Technical Support Team over 1 year

    I want to rename the image name which is a pickedfile type when i'm uploading that image to the server. when it's uploading image name automatically changes like this scaled_image_picker825656736600296027.jpg

    getting image code :-

      void takePhoto(ImageSource source) async {
        final pickedFile = await _picker.getImage(
          source: source,
          imageQuality: 20,
        );
        setState(() {
          _imageFile = pickedFile;
        });
      }
    

    uploading code is :-

    void UploadImage() async {
        var request = http.MultipartRequest('POST', Uri.parse(URLs.image));
        request.fields['CompanyCode']=  "${widget.CompanyCode}";
        request.fields['empCode'] ='$user_id';
        request.fields['documentType']="POD";
        if (_imageFile != null && widget.image == null) {
          print('Original path: ${_imageFile.path}');
          String dir = path.dirname(_imageFile.path);
          String newPath = path.join(dir, widget.docketno + '-${DateTime.now()}.jpg');
          print('NewPath: ${newPath}');
           _imageFile.renameSync(newPath); // renameSync is showing error
          request.files
              .add(await http.MultipartFile.fromPath('file',_imageFile.path));
        }
        else if(_imageFile ==null && widget.image != null){
          print('AddingUserProfilePic ${widget.image.path}');
          request.files.add(await http.MultipartFile.fromPath('file', widget.image.path));
        }
        var response = await request.send();
        var responseBody = await http.Response.fromStream(response);
        myData = json.decode(responseBody.body);
        print('Status code is : ${response.statusCode}  && ${response}');
        if (response.statusCode == 200) {
          responseResult = myData['filePath'];
          print('the filepath is ' + responseResult.substring(60));
          image_path = responseResult.substring(60);
          Fluttertoast.showToast(msg: 'Uploaded Successfully');
          Navigator.push(context, MaterialPageRoute(builder: (context) => delivered(CompanyCode : widget.CompanyCode,docketno :widget.docketno,Year: widget.Year,
              drsno:widget.drsno,challan: widget.challan , package :widget.package , response : image_path)));
          // Navigator.popUntil(context, (route) {
          //   return count++ == 2;
          // });
        } else {
          Fluttertoast.showToast(msg: 'Upload Failed');
        }
      }
    

    renameSync is showing the error that The method 'renameSync' isn't defined for the type 'PickedFile'. so how can i rename my image file????

  • Technical Support Team
    Technical Support Team over 3 years
    but it still prints the same path
  • Technical Support Team
    Technical Support Team over 3 years
    an error also occurred Unhandled Exception: FileSystemException: Cannot retrieve length of file, path = '/storage/emulated/0/Android/data/com.example.flutter_app/fi‌​les/Pictures/scaled_‌​image_picker32585635‌​3267804129.jpg' (OS Error: No such file or directory, errno = 2)
  • zhpoo
    zhpoo over 3 years
    After the file renamed, you should use the newPath file to do the things you want, becase the old file(_imageFile.path) is not exist any more.
  • Technical Support Team
    Technical Support Team over 3 years
    but the newPath is string and in fromPath only files are uploading
  • zhpoo
    zhpoo over 3 years
    You have the path already, then just use File(newPath) - this is a file.
  • Technical Support Team
    Technical Support Team over 3 years
    Status code is : 500 and the circular progress indicator is continously loading