Does not allow to save image to local directory in flutter in android 10

557 views Asked by At

I'm trying to save an image to phone local directory. Here I use image_downloader package for that. Here's my code

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());
    }
  }

}

When I run on Android 10 device and try to download an image it gives me this error,

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

I already added android:requestLegacyExternalStorage="true" in AndroidManifest and set targetSdkVersion and compileSdkVersion to 29 in build.gradle file. I did flutter clean and run the code but issue remain the same.

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

I would be really grateful if anyone can help me with this issue. Thanks !

2

There are 2 answers

1
Akif On

There are only 4 standard directories in this package.

  /// 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");

So, you have to change here:


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

I advise you to keep it simple like this:


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

0
Sneh Modi On

I faced the same issue with Android 10, I think the feature of saving in custom directory is not compatible with Android10 . The same code works perfectly for others