How to change a image file to thumbnail while keeping original file in flutter

浪子不回头ぞ 提交于 2019-12-11 07:47:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!