1

I am trying to add this future to my app so I wrote this code

 PickedFile _image; String _uploadedFileURL; final _picker = ImagePicker(); Future getImage(bool isCamera) async { PickedFile image; if (isCamera) { PickedFile image = await _picker.getImage(source: ImageSource.camera); setState(() { _image = image; }); } else PickedFile image = await _picker.getImage(source: ImageSource.gallery); setState(() { _image = image; }); } 

but how to uploaded it in flutter ? I searched a lot but in every code I found there is a mistake because of the updated and in the storage package example I did not find uploading image so, what the last code to upload images?

3 Answers 3

2

This is what I have been using for selecting the image from image picker then cropping the picked image and then later on uploading it into firebase storage. Hope this helps you. Any clarifications needed please comment.

 // Crop Selected Image Future _cropImage(File selectedFile) async { File cropped = await ImageCropper.cropImage( sourcePath: selectedFile.path, aspectRatio: CropAspectRatio( ratioX: 1.0, ratioY: 1.0, ), cropStyle: CropStyle.circle, ); if (cropped != null) { setState( () { _imageFile = cropped; }, ); } } // Select Image Via Image Picker Future getImage(ImageSource source) async { // ignore: deprecated_member_use File selected = await ImagePicker.pickImage(source: source); if (selected != null) { _cropImage(selected); } } // Upload Picture to Firebase Future uploadImage(BuildContext context) async { String fileName = basename(_imageFile.path); Reference firebaseStorageRef = FirebaseStorage.instance.ref().child(fileName); UploadTask uploadTask = firebaseStorageRef.putFile(_imageFile); // ignore: unused_local_variable TaskSnapshot taskSnapshot = await uploadTask; } 
Sign up to request clarification or add additional context in comments.

Comments

1

To select the image from gallery(modify as per your need):-

selectImageFromGallery() async { final picker=ImagePicker(); setState(() { inProcess=true; }); final imageFile= await picker.getImage(source: ImageSource.gallery); if(imageFile!=null) { _image=File(imageFile.path); } setState(() { inProcess=false; }); } 

And to upload use the below code:-

Future<String> uploadFile(File image) async { String downloadURL; String postId=DateTime.now().millisecondsSinceEpoch.toString(); Reference ref = FirebaseStorage.instance.ref().child("images").child("post_$postId.jpg"); await ref.putFile(image); downloadURL = await ref.getDownloadURL(); return downloadURL; } 

to save data:-

saveData()async { String url=await uploadFile(_image);//to upload and store the url //rest code to save to firestore. } 

Comments

0

Slightly off-topic: Upload image picker file to Google Cloud SQL / AWS PostgresQL, after converting it to bytes

I can only help you on the side related to uploading image picker files converted to byte to PostgresQL.

I upload image as bytes to my PostgresQL DB, in the functions below I gather all the PickedFile and I upload them all at once while generating unique UID. Once they have been uploaded I save their uid to link them to the user who uploaded.

See the code below:

import 'dart:async'; import 'dart:io'; import 'dart:typed_data'; import 'dart:convert'; import 'package:image_picker/image_picker.dart'; import 'dart:math'; import 'package:postgres/postgres.dart'; import 'package:convert/convert.dart'; final Random _random = Random.secure(); // generate uid for your image String createCryptoSecureId([int length = 32]) { var values = List<int>.generate(length ~/ 1.3, (i) => _random.nextInt(256)); return base64Url.encode(values); } // works with list of PickedFile Future<String> uploadImagesToPSQL(final List<PickedFile> imageFileList) async { List<Uint8List> imageBytesList = []; for (PickedFile imageFile in imageFileList) { File file = File(imageFile.path); if (file != null) { Uint8List imageBytes = file.readAsBytesSync(); print(imageBytes); imageBytesList.add(imageBytes); } } String sqlQuery = "INSERT INTO myimages(id, image) VALUES (@id, decode(@image, 'hex'))"; PostgreSQLConnection connection = await initConnectionPSQL('update'); // can't share as there is sensitive code. List<String> imageIds; final futures = <Future>[]; for (var imageBytes in imageBytesList) { String imageId = createCryptoSecureId(32); imageIds.add(imageId); var substitutionValues = {"id": imageIds, "image": hex.encode(imageBytes)}; futures.add( connection.execute(sqlQuery, substitutionValues: substitutionValues)); } await Future.wait(futures); return imageIds.join(); } 

NB: This is test code, ideally you want to use a REST api that has secure access to your DB.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.