upload and get image from firebase storage flutter web - flutter

I am currently developing a web filter application
In the application, when I enter product details, I need to choose an image of the product
But I face a problem, when I choose an image from the computer, I cannot get the image path, the name and the bytes only, and when I upload the bytes to the firebase, I cannot get the image because it is bytes. How can I calculate the path of the image and display it in a list?I am currently developing a flutter web application
In the application, when I enter product details, I need to choose an image for the product from my PC
But I face a problem, when I choose an image from the computer, I cannot get the image path, the (i get name and the bytes only), and when I upload the bytes to the firebase, I cannot get the image because it is bytes not as path. How can I get the path of the image and display it in a list products?
Uint8List? fileBytes;
String? fileName;
String? picUrl;
void chooseImage() async {
FilePickerResult? result = await FilePicker.platform.pickFiles();
if (result != null) {
fileBytes = result.files.first.bytes;
fileName = result.files.first.name;
}
}
uploadImageToFirebase() async {
UploadTask _uploadTask = (await FirebaseStorage.instance
.ref('uploads/$fileName')
.putData(fileBytes!)) as UploadTask;
picUrl = await (await _uploadTask).ref.getDownloadURL();
}

Related

Uploading audio to firebase flutter times out

I am trying to upload an audio file to firebase within my flutter app.
The user flow is filePicker -> confirm screen (where the user picks the details of the song like name, caption, etc). If I wait too long in the confirm screen (longer than the length of the audio file), the file "times out" and returns false for file.existsSync().
However, whenever I run through the confirm screen super fast, it works because I uploaded it before the length of the audio file finishes. I assume this has to do with caching, but I have no idea how to fix this.
My code for uploading is below:
Future<String> _uploadSongToStorage(String id, String songPath) async {
Reference ref = firebaseStorage.ref().child('songs').child(id);
//assert(songFile.existsSync());
UploadTask uploadTask = ref.putFile(File(songPath));
TaskSnapshot snap = await uploadTask;
String downloadUrl = await snap.ref.getDownloadURL();
return downloadUrl;}

In file picker in flutter, path was not unique

I'm trying with two different images with same name. But the path was same for two picked images. it was not unique.So that I uploaded in server second image with the same name of first uploaded image. But server had both the image are same and it had first image. So how to handle this case and customize the path?
You can use the path_provider to define the customize directory on your app.
So, copy the file with your customize path and rename the file name.
BTW, do NOT save the absolute path of File on iOS.
The iOS use SandBox to access the file. When you get the file path every time. The file path will be different.
class FileUtils {
final String avatarPath = '/avatar/';
Future<String> getAvatarDirectoryPath() async {
final String appDirPath = await getApplicationSupportDirectory().path;
final Directory avatarDirPath = await Directory(appDirPath + avatarPath).create();
return directory.path;
}
}
// Example
{
final XFile? image = await ImagePicker().pickImage(source: ImageSource.camera);
final File imageFile = File(image.path);
final File newFile = File(await FileUtils().getAvatarDirectoryPath() + 'userAvatar.png');
await imageFile.copy(newFile.path);
}

Uploading network image to firestore

How can I download an image file from network using a given url and upload it to fire store?
I am having hard time downloading image from url and uploading it to fire store.
you could do it for example like this:
upload() async {
//pick image use ImageSource.camera for accessing camera.
File image = await ImagePicker.pickImage(source: ImageSource.gallery);
//alternative with ressource in your app:
var src = // your image src
File image = await Image.network(src);
//basename() function will give you the filename
String fileName = basename(image.path);
//passing your path with the filename to Firebase Storage Reference
StorageReference reference =
FirebaseHelper.firebaseStorage().child("your_path/$fileName");
//upload the file to Firebase Storage
StorageUploadTask uploadTask = reference.putFile(image);
//Snapshot of the uploading task
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
}
To get a better understanding of firebase storage and the upload process, and features like showing progress and more I recommend you checking this useful article too: https://medium.com/flutterpub/firebase-cloud-storage-and-flutter-fa2e91663b95

How to Share Image from API(URL) in flutter?

Here I want to share image which I get through API. I tried different method for this functionality but I did not get any solution because every solutions have for only one image.
Exactly, I get multiple image from Url and I open any particular image in next page. So, I want to share that image which I opened in another page.
I tried for sharing image but I could not did this. Whenever I try to share that image, Image url share with sharing option on device but I want share image not URl of image How I can accomplish this?
You have to download the image like this:
http.Response response = await http.get(url);
then create an image on the device using the downloaded image like this (Read and Write Files):
final directory = await getTemporaryDirectory();
final path = directory.path;
final file = File('$path/image.png');
file.writeAsBytes(response.bodyBytes);
The getTemporaryDirectory() is in the plugin path_provider. Now, you have the image you'd like to share stored in the temporarily as "image.png" and you can use the share_plus plugin to share the image like this:
Share.shareFiles(['$path/image.png']);
Thanks to #abdulrazak, At first, you have to download the image in a temporary path & then you can share the image as you want. used Dio to download the image & share_plus for sharing
void _shareNetworkImage(String url) async {
Directory tempDir = await getTemporaryDirectory();
final path = '${tempDir.path}/test.jpeg';
await Dio().download(url, path);
Share.shareFiles([path]);
}
You can use the esys_flutter_share plugin.
Install it, and then get dependencies:
dependencies:
esys_flutter_share: ^1.0.2
Import the plugin to your code:
import 'package:esys_flutter_share/esys_flutter_share.dart';
Use the below code:
var request = await HttpClient().getUrl(Uri.parse('https://yourImageURL.jpg'));
var response = await request.close();
Uint8List bytes = await consolidateHttpClientResponseBytes(response);
await Share.file('ESYS AMLOG', 'amlog.jpg', bytes, 'image/jpg');

where do I need to store photo images in Flutter App?

I have an app that takes a photo.
I need that photo to be stored where all my assets/images/photos are.
So basically my question is :
a) how can I find the path where these assets are (on the phone)?
b) how can I add files to that same path?
Thanks
As per my knowledge you can not store image in assets package. Just because of when we build android/ios app after that assets folder is read only.
You need to store image in local mobile storage OR cloud OR catch memory
1) You can get storage path using :
Future<String> getStorageDirectory() async {
if (Platform.isAndroid) {
return (await getExternalStorageDirectory()).path; // OR return "/storage/emulated/0/Download";
} else {
return (await getApplicationDocumentsDirectory()).path;
}
}
2) Add image in path
createImage() async{
String dir= getStorageDirectory();
File directory = new File("$dir");
if (directory.exists() != true) {
directory.create();
}
File file = new File('$directory/image.jpeg');
var newFile = await file.writeAsBytes(/* image bytes*/);
await newFile.create();
}