I want to upload image to firebase storage from a link using flutter so if anyone can tell me how can i do that. Right now i am using imagePicker to pick images from mobile phone but now i wanted to upload the pics from URL i have searched for it and there is answer for javascript or other but not for flutter and i want it in flutter Thanks
CodePudding user response:
You have to first download the image from the link and save it in you temporary directory and then upload into firebase as you are already uploading it from image picker here is the sample code that will help you to understand better
download(String url, String fileName) async {
try {
var per = await askPermission();
if (per!.isGranted) {
const path =
'/storage/emulated/0/Download/'; //you can use temporary
directory
final file = File('$path/$fileName');
await dio!.download(siteUrl url, file.path,
onReceiveProgress: (rec, total) {
isLoading = true;
print(rec);
});
Get.snackbar('Success', 'File downloaded successfully',
backgroundColor: Colors.green.withOpacity(0.4));
} else {
Get.snackbar('Error', 'please grant storage permission',
backgroundColor: Colors.red.withOpacity(0.4));
}
} catch (e) {
pr.close();
Get.snackbar('Error', '${e.toString}',
backgroundColor: Colors.red.withOpacity(0.4));
print(e);
}
}
i use dio and Permission handler packages
CodePudding user response:
from your linked Question, You can store the urls in your database else if you really want to store them in your firebase storage you have to download them and save to firebase. You can use plugins like flutter_downloader, or use dio /http to download then upload to firebase
CodePudding user response:
this has worked for me `
class ImageUrlToStorage extends StatefulWidget {
const ImageUrlToStorage({Key key}) : super(key: key);
@override
State<ImageUrlToStorage> createState() => _ImageUrlToStorageState();
}
class _ImageUrlToStorageState extends State<ImageUrlToStorage> {
Dio dio;
bool isLoading = false;
final url =
'https://images.pexels.com/photos/733853/pexels-photo-733853.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500';
_save() async {
var status = await Permission.storage.request();
if (status.isGranted) {
var response = await Dio()
.get(url, options: Options(responseType: ResponseType.bytes));
final result = await ImageGallerySaver.saveImage(
Uint8List.fromList(response.data),
quality: 60,
name: "hello");
print(result);
}
}
@override
Widget build(BuildContext context) {
return ButtonWidget(
onClicked: () {
_save();
},
text: 'Upload Image to Storage',
);
}
}
` I have used dio,image_gallery_saver,and Permission_handler