【问题标题】:How to change the image_picker file name [duplicate]如何更改 image_picker 文件名 [重复]
【发布时间】:2021-12-15 21:40:29
【问题描述】:

如果image_picker库返回存在的文件路径,如何直接修改?

void getImage(ImageSource imageSource) async {
  final pickedFile = await ImagePicker().pickImage(source: imageSource);
  if (pickedFile != null) {
    pickedFile.path; // how to rename?
  }
}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    使用路径包。

    import 'package:path/path.dart' as path;
    

    然后创建一个新的目标路径来重命名文件。

    File pickedFile = await ImagePicker().pickImage(source: imageSource);
      if (pickedFile != null) {
        pickedFile.path; // how to rename?
      }
    
    String dir = path.dirname(pickedFile.path);
    String newName = path.join(dir, 'what ever name you want.jpg');
    pickedFile.renameSync(newName);
    

    【讨论】:

    • pickedFile.renameSync(newName); 必须是File(pickedFile.path).renameSync(newName),谢谢。
    猜你喜欢
    • 2016-02-29
    • 2017-08-29
    • 2018-07-01
    • 2013-10-06
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 2011-12-31
    • 2013-02-25
    相关资源
    最近更新 更多