![](/img/trans.png)
[英]How to download a file from url and save the file in asset folder in flutter?
[英]Download image from url and save in sd card folder in flutter
我想從 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);
}
由於 path_provider 不提供下載目錄路徑。 您將需要調用本機 API。
使用這些包來做到這一點: -
downloads_path_provider或ext_storage
如果是第一個,代碼將如下所示:-
var documentDirectory = await DownloadsPathProvider.downloadsDirectory;
如果是第二個,代碼將如下所示:-
var documentDirectory = await ExtStorage.getExternalStoragePublicDirectory(ExtStorage.DIRECTORY_DOWNLOADS);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.