我正在尝试将图像保存到手机本地目录。在这里,我使用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">
如果有人可以帮助我解决这个问题,我将非常感谢。谢谢!
答案 0 :(得分:1)
我在Android 10上也遇到了同样的问题,我认为保存在自定义目录中的功能与Android10不兼容。相同的代码对其他人非常有用
答案 1 :(得分:0)
此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}'),