【问题标题】:Does not allow to save image to local directory in flutter in android 10不允许在 android 10 中将图像保存到本地目录
【发布时间】:2021-01-29 07:48:38
【问题描述】:

我正在尝试将图像保存到手机本地目录。在这里,我使用 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。我确实清理并运行了代码,但问题仍然存在。

build.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">

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

【问题讨论】:

    标签: flutter flutter-dependencies flutter-packages flutter-image


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      这个package中只有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}'),
      
      

      【讨论】:

      • 感谢您的回答。是的,它以这种方式下载图像,但它存储在“/storage/emulated/0/Android/data//files`”中。但我想在手机图库中看到下载的图像。因此,我必须将图像保存在下载文件夹或任何其他指定的文件夹中。你能给我一个解决方案吗?如果您能帮助我解决这个问题,我将不胜感激@Akif
      猜你喜欢
      • 2015-07-04
      • 2012-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多