我需要从我的相机应用程序中拍照并将照片上传到我的 Firebase 存储中。同时我还需要上传相同图像文件的缩略图版本。我需要获取图像的 url 并将其存储到数据库中。我是 Firebase 的新手,正在为一个大学项目工作。我需要缩略图,以便在加载图像时,首先我需要加载缩略图,然后根据条件我将加载全尺寸图像
Future getImage() async{
var tempImage = await ImagePicker.pickImage(source: ImageSource.camera,imageQuality: 30);
setState(() {
sampleImage=tempImage;
Im.Image image = decodeImage(sampleImage.readAsBytesSync());
Im.Image thumbnail = copyResize(image, width: 120,height: 120);
imageThumb=thumbnail as File;
});
}
bool validateAndSave(){
final form = formKey.currentState;
if(form.validate()){
form.save();
return true;
}else{return false;}
}
void uploadStatusImage() async{
goToHomePage();
if(validateAndSave()){
final StorageReference postImageRef= FirebaseStorage.instance.ref().child("Post Images");
var timeKey=new DateTime.now();
final StorageUploadTask uploadTask=postImageRef.child(timeKey.toString()+".jpg").putFile(sampleImage);
final StorageUploadTask uploadTaskThumb=postImageRef.child(timeKey.toString()+"thumb.jpg").putFile(imageThumb);
var ImageUrl = await(await uploadTask.onComplete).ref.getDownloadURL();
var ImageThumbUrl = await(await uploadTaskThumb.onComplete).ref.getDownloadURL();
this.url=ImageUrl.toString();
this.url1=ImageThumbUrl.toString();
print("Image Url= "+url);
saveTodatabase(url,url1);
}
}
void saveTodatabase(url,url1) async {
var dbTimekey=new DateTime.now();
var formatDate=new DateFormat('MM d, yyyy');
var formatTime=new DateFormat('EEEE, hh:mm aaa');
String date=formatDate.format(dbTimekey);
String time=formatTime.format(dbTimekey);
var data = {
"image":url,
"imagethumb":url1,
"description": _myValue,
"date": date,
"time": time,
};
await databaseReference.collection("posts")
.document(dbTimekey.toString())
.setData(data);
//ref.child("Posts").push().set(data);
}