flutter 中的“image_picker”包打开随机文件夹

问题描述 投票:0回答:2

我正在尝试在我的 flutter 项目中使用“image_picker”包从 Android 设备的图库中选取图像。

但这会默认打开一些随机文件夹,同时尝试选择图像而不是最近或下载文件夹。

如何配置图像选择器每次打开特定文件夹?例如 - 下载文件夹。

这是我用于选择图像的按钮的颤振代码。

GestureDetector(
                        onTap: () async {
                          _animationController.stop();

                          /// waiting for a image to be picked from the local
                          /// storage
                          XFile? image = await imagePicker.pickImage(source: ImageSource.gallery, imageQuality: 100);

                          setState(() {
                            /// get the file path of the local file the user has picked.
                            /// setting the state to show the uploading effect
                            _image = File(image!.path);
                          });

                          /// uploading the image
                          await _profileProviderRead.sendImage(image, widget.photoAlbumName, context);
                        },
                        child: Container(
                          padding: EdgeInsets.all(16.h),
                          decoration: BoxDecoration(
                            gradient: kGradient1,
                            borderRadius: BorderRadius.circular(16.r),
                          ),
                          child: const Icon(
                            Icons.add_rounded,
                            color: kWhiteColor,
                            size: 32,
                          ),
                        ),
                      ```
flutter dart
2个回答
0
投票
    The "image_picker" package doesn't have a built-in option to directly open a specific folder like the Downloads folder.
    
    However, you can indirectly achieve this behavior by using a file picker library alongside the "image_picker" package. The file picker library allows you to pick files from specific directories, including the Downloads folder.

1.) Add the "file_picker" package to your pubspec.yaml file.
2.)Modify your button's onTap function to use the file picker to open the Downloads folder:

GestureDetector(
  onTap: () async {
    _animationController.stop();

    /// Use the file picker to open the Downloads folder and pick a file.
    FilePickerResult? result = await FilePicker.platform.pickFiles(type: FileType.image);

    if (result != null) {
      /// Get the file path of the selected file and create a File object from it.
      String? imagePath = result.files.single.path;
      if (imagePath != null) {
        setState(() {
          _image = File(imagePath);
        });
      }

      /// Upload the image.
      await _profileProviderRead.sendImage(_image, widget.photoAlbumName, context);
    } else {
      // User canceled the file picking.
    }
  },
  child: Container(
    padding: EdgeInsets.all(16.h),
    decoration: BoxDecoration(
      gradient: kGradient1,
      borderRadius: BorderRadius.circular(16.r),
    ),
    child: const Icon(
      Icons.add_rounded,
      color: kWhiteColor,
      size: 32,
    ),
  ),
),


By using the "file_picker" package with type: FileType.image, you can open the file picker directly in the Downloads folder or any other folder containing images. The user can then select an image, and you can proceed with the rest of your logic to upload the image.

0
投票

image_picker 的 Android 实现使用 image_picker_android。当你直接运行image_picker_android提供的demo时,你会发现可以直接打开相册页面。 因为useAndroidPhotoPicker在image_picker_android中默认是开启的,而在image_picker中默认是false。目前还没有办法直接通过image_picker来设置useAndroidPhotoPicker。 您可以在image_picker示例代码中引入image_picker_android,然后设置useAndroidPhotoPicker。设置完成后,再次运行image_picker示例代码即可直接打开相册页面

https://pub.dev/packages/image_picker_android

© www.soinside.com 2019 - 2024. All rights reserved.