【发布时间】:2021-05-08 14:54:16
【问题描述】:
我想将从图库中选择的图片上传到 Firestore 存储,这是我的第一次,但我阅读了文档并在互联网上查看,这就是我写的(它可以正常工作,但在存储上什么也没做) .
下面的代码,我测试了 imgFromGallery 并且它可以工作,可能问题是存储功能。
Future<File> imgFromGallery() async {
try {
final ImagePicker _picker = ImagePicker();
final PickedFile imageFile =
await _picker.getImage(source: ImageSource.gallery, imageQuality: 50);
//If there is no image selected, return.
if (imageFile == null) return null;
//File created.
File tmpFile = File(imageFile.path);
//it gives path to a directory - path_provider package.
final appDir = await getApplicationDocumentsDirectory();
//filename - returns last part after the separator - path package.
final fileName = tmpFile.path.split('/').last;
//copy the file to the specified directory and return File instance.
return tmpFile = await tmpFile.copy('${appDir.path}/$fileName');
} catch (e) {
print(e);
return null;
}
}
//保存文件到Storage函数
Future<void> setPicture(String pathStorage) async {
try {
final File file = await imgFromGallery();
if (file == null) return;
FirebaseStorage.instance.ref(pathStorage).putFile(file);
return;
} catch (e) {
print(e);
}
}
附: : 我想把上传图片的URL作为“setPicture”的返回,但是我还是不知道怎么做。
【问题讨论】:
标签: firebase flutter firebase-storage