我想从URL下载图像并将其保存到SD文件夹中。
我尝试过的事情:
我尝试过此代码,它可以正常工作,但它存储在
/storage/emulated/0/Android/data/com.example.college_services/files/Images
但我想存储在/Sdcard/Images
_dowloadimg() async{
var url = widget.imageURL;
var response = await get(url);
var documentDirectory = await getExternalStorageDirectory();
var firstPath = documentDirectory.path + "/Images";
var filePathAndName = documentDirectory.path + '/Images/img_${i++}.jpg';
await Directory(firstPath).create(recursive: true);
File file2 = new File(filePathAndName);
file2.writeAsBytesSync(response.bodyBytes);
}
答案 0 :(得分:0)
由于path_provider不提供下载目录路径。您将需要调用本机api。
使用这些软件包可以做到这一点:-
downloads_path_provider 要么 ext_storage
如果是第一个,代码将如下所示:-
var documentDirectory = await DownloadsPathProvider.downloadsDirectory;
如果是第二个,代码将如下所示:-
var documentDirectory = await ExtStorage.getExternalStoragePublicDirectory(ExtStorage.DIRECTORY_DOWNLOADS);