问题
I need to take a picture from my camera app and upload the photo to my firebase storage. In the same time I also need to upload the thumbnail version of same image file. I need to fetch the url of the image and store it to database. I am new to firebase and working for a college project. I need thumbnail so that when loading the image, first i need to load the thumbnail and as per condition i will load the full size image
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);
}
来源:https://stackoverflow.com/questions/57542155/how-to-change-a-image-file-to-thumbnail-while-keeping-original-file-in-flutter