Jee*_*sta 5 dart flutter imagepicker
以下代码用于将任何图像从图库/相机上传到 Firebase 存储。我成功地将图像与元数据一起上传到存储。现在的问题是我无法获取上传图像的下载 URL。尝试了很多但没有找到任何解决方案。
FirebaseStorage storage = FirebaseStorage.instance;
final picker = ImagePicker();
PickedFile pickedImage;
File imageFile;
Future<void> _upload(String inputSource) async {
try {
pickedImage = await picker.getImage(
source: inputSource == 'camera'
? ImageSource.camera
: ImageSource.gallery,
maxWidth: 1920);
final String fileName = path.basename(pickedImage.path);
imageFile = File(pickedImage.path);
try {
// Uploading the selected image with some custom meta data
await storage.ref(fileName).putFile(
imageFile,
SettableMetadata(
customMetadata: {
'uploaded_by': 'A bad guy',
'description': 'Some description...'
},
),
);
// Refresh the UI
setState(() {});
} on FirebaseException catch (error) {
print(error);
}
} catch (err) {
print(err);
}
}
Run Code Online (Sandbox Code Playgroud)
希望您一切顺利\xe2\x80\xa6\n您可以尝试此方法将图像(任何文件)的 URL 从 Firebase 存储获取到 Firebase 存储,然后您可以检索图像。
\nclass _UploadAdState extends State<UploadAdPage> {\n final formKey = GlobalKey<FormState>();\n File _myimage;\n String imgUrl;\n\n Future getImage1(File chosenimage) async {\n PickedFile img =\n await ImagePicker.platform.pickImage(source: ImageSource.gallery);\n\n if (chosenimage == null) return null;\n\n File selected = File(img.path);\n setState(() {\n _myimage = chosenimage;\n });\n}\n\n// changing the firestore rules and deleteing if request.auth != null;\nsendData() async {\n// to upload the image to firebase storage\nvar storageimage = FirebaseStorage.instance.ref().child(_myimage.path);\nUploadTask task1 = storageimage.putFile(_myimage);\n\n// to get the url of the image from firebase storage\nimgUrl1 = await (await task1).ref.getDownloadURL();\n// you can save the url as a text in you firebase store collection now\n}\n}\n
Run Code Online (Sandbox Code Playgroud)\n