简体   繁体   English

从 url 下载图像并保存在 flutter 的 sd 卡文件夹中

[英]Download image from url and save in sd card folder in flutter

I want to download an image from URL and save it into the SD folder.我想从 URL 下载图像并将其保存到 SD 文件夹中。

what I have tried:我尝试过的:

I have tried this code it works fine but it store in /storage/emulated/0/Android/data/com.example.college_services/files/Images but i want to store in /Sdcard/Images我试过这个代码它工作正常,但它存储在/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);

  }

As path_provider doesn't provide download directory path.由于 path_provider 不提供下载目录路径。 You will need to call native apis.您将需要调用本机 API。

Use These packages to do that:-使用这些包来做到这一点: -

downloads_path_provider OR ext_storage downloads_path_providerext_storage

In case of 1st one the code will look like this:-如果是第一个,代码将如下所示:-

var documentDirectory = await DownloadsPathProvider.downloadsDirectory;

In case of 2nd one the code will look like this:-如果是第二个,代码将如下所示:-

var documentDirectory = await ExtStorage.getExternalStoragePublicDirectory(ExtStorage.DIRECTORY_DOWNLOADS);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM