我正在尝试使用networkimage()从服务器加载图像,并且一旦加载就想下载该图像。.有人可以建议我一些想法吗。
CircleAvatar(
backgroundImage: NetworkImage(url),
maxRadius: 15.0,
);
这里我正在从服务器加载图像。我想在图像加载后将图像保存到特定路径。
答案 0 :(得分:2)
我使用了image_downloader。
使用image_downloader软件包方法中的await ImageDownloader.downloadImage("url")
,使用其URL下载图像。
注意:以上方法将返回如下值:-
已成功保存的图像的图像编号。
如果没有被授予权限,则为null。 为此,您必须请求存储权限,只需将以下行添加到android清单文件中即可:
使用权限 android:name =“ android.permission.WRITE_EXTERNAL_STORAGE”
答案 1 :(得分:1)
我使用此插件通过URL将图片保存在手机中 https://pub.dartlang.org/packages/image_picker_saver
答案 2 :(得分:1)
我推荐image_downloader。
Environment.DIRECTORY_DOWNLOADS
或指定位置。通过调用inExternalFilesDir()
,不需要指定权限。callback()
,您可以获取进度状态。以下是最简单的示例。它将被保存。
await ImageDownloader.downloadImage(url);
答案 3 :(得分:1)
我把这个答案放在这里是因为我在使它工作时遇到很多问题,希望对下一个人有所帮助。
我制作了这个测试程序,可以从网上下载图片,将其存储在设备本地路径中,然后在运行时显示它:
php artisan config:cache
这是我的pubspec.yaml文件:
import 'package:flutter/material.dart';
import 'package:http/http.dart' show get;
import 'dart:io';
import 'package:path_provider/path_provider.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Test Image',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Test Image'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
initState() {
_asyncMethod();
super.initState();
}
_asyncMethod() async {
//comment out the next two lines to 'prove' the next time that you run
// the code to prove that it was downloaded and saved to your device
var url = "https://www.tottus.cl/static/img/productos/20104355_2.jpg";
var response = await get(url);
var documentDirectory = await getApplicationDocumentsDirectory();
var firstPath = documentDirectory.path + "/images";
var filePathAndName = documentDirectory.path + '/images/pic.jpg';
//comment out the next three lines to 'prove' the next time that you run
// the code to prove that it was downloaded and saved to your device
await Directory(firstPath).create(recursive: true);
File file2 = new File(filePathAndName);
file2.writeAsBytesSync(response.bodyBytes);
setState(() {
imageData = filePathAndName;
dataLoaded = true;
});
}
String imageData;
bool dataLoaded = false;
@override
Widget build(BuildContext context) {
if (dataLoaded) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.file(File(imageData), width: 600.0, height: 290.0)
],
),
),
);
} else {
return CircularProgressIndicator(
backgroundColor: Colors.cyan,
strokeWidth: 5,
);
}
}
}
答案 4 :(得分:0)
答案 5 :(得分:0)
您可以使用此功能从相机上传并获取Url(下载)图像
Future _upIm()async {
final StorageReference firebaseStorageRef =
FirebaseStorage.instance.ref().child(DateTime.now().toString());
final StorageUploadTask task =
firebaseStorageRef.putFile(_storedImage);
var downUrl=await (await task.onComplete).ref.getDownloadURL();
var url =downUrl.toString();
print(url);
setState(() {
uploadImage=url;
});
}
答案 6 :(得分:0)
我尝试了很多解决方案,但这是对我来说最简单的解决方案......试试吧
步骤 - 1
将此包添加到您的 pubspec.yaml
文件
dependencies:
image_downloader: ^0.20.1
步骤 - 2
将此添加到您的dart
文件
import 'package:image_downloader/image_downloader.dart';
步骤 - 3
在按下 download
按钮
ColButton(
title: 'Download',
icon: Icons.file_download,
onTap: () async {
try {
showLoadingDialog(context);
// Saved with this method.
var imageId =
await ImageDownloader.downloadImage("https://raw.githubusercontent.com/wiki/ko2ic/image_downloader/images/bigsize.jpg");
if (imageId == null) {
return;
}
// Below is a method of obtaining saved image information.
var fileName = await ImageDownloader.findName(imageId);
var path = await ImageDownloader.findPath(imageId);
var size = await ImageDownloader.findByteSize(imageId);
var mimeType = await ImageDownloader.findMimeType(imageId);
Navigator.pop(context);
showToast('Image downloaded.');
} on PlatformException catch (error) {
print(error);
}
},
),