0

我正在尝试将图像保存到手机本地目录。在这里,我使用image_downloader包。这是我的代码

class DownloadImage {

  Future writeToDownloadPath(GridImage imageData) async {

    try {

      var imageId = await ImageDownloader.downloadImage(imageData.url
      ,destination: AndroidDestinationType.custom(directory: 'motivational Quotes', 
      inPublicDir: true ,subDirectory: '/motivational Quotes${imageData.location}'));

      print('imageId' + imageId.toString());

      if (imageId == null) {
        return;
      }

    } catch(e) {
      print(e.toString());
    }
  }

}

当我在 Android 10 设备上运行并尝试下载图像时,它给了我这个错误,

E/MethodChannel#plugins.ko2ic.com/image_downloader(25282): java.lang.IllegalStateException: Not one of standard directories: motivational Quotes

我已经在 AndroidManifest 中添加了 android:requestLegacyExternalStorage="true" 并在 build.gradle 文件中将 targetSdkVersion 和 compileSdkVersion 设置为 29。我确实清理干净并运行了代码,但问题仍然存在。

构建.gradle

defaultConfig {

    applicationId "com.example.app
    minSdkVersion 21
    targetSdkVersion 29
    versionCode flutterVersionCode.toInteger()
    versionName flutterVersionName
    multiDexEnabled true
}

AndroidManifest

<application
      android:name="io.flutter.app.FlutterApplication"
      android:label="Motivational Quotes"
      android:icon="@mipmap/ic_launcher"
      android:requestLegacyExternalStorage="true">

如果有人能帮助我解决这个问题,我将不胜感激。谢谢 !

4

2 回答 2

1

我在 Android 10 上遇到了同样的问题,我认为保存在自定义目录中的功能与 Android10 不兼容。相同的代码对其他人完美运行

于 2020-11-05T06:10:35.150 回答
0

此包中只有 4 个标准目录。

  /// Environment.DIRECTORY_DOWNLOADS
  static final directoryDownloads =
      AndroidDestinationType._internal("DIRECTORY_DOWNLOADS");

  /// Environment.DIRECTORY_PICTURES
  static final directoryPictures =
      AndroidDestinationType._internal("DIRECTORY_PICTURES");

  /// Environment.DIRECTORY_DCIM
  static final directoryDCIM =
      AndroidDestinationType._internal("DIRECTORY_DCIM");

  /// Environment.DIRECTORY_MOVIES
  static final directoryMovies =
      AndroidDestinationType._internal("DIRECTORY_MOVIES");

所以,你必须在这里改变:


 var imageId = await ImageDownloader.downloadImage(imageData.url
      ,destination: AndroidDestinationType.custom(directory: 'motivational Quotes', 
      inPublicDir: true ,subDirectory: '/motivational Quotes${imageData.location}'));

我建议您保持简单,如下所示:


 destination: AndroidDestinationType.directoryPictures
                        ..inExternalFilesDir()
                        ..subDirectory('${imageData.location}'),

于 2020-10-14T21:06:24.440 回答