As I said in the title, I'm trying to share files converting an image file to PNG format. I have tried to use the packages share_plus and social_share. I have this code:
img.Image? image = img.decodeImage(File(imageXFile.path).readAsBytesSync());
final imageFile = File('${appDir.path}/$fileName.png');
imageFile.writeAsBytesSync(img.encodePng(image));
setState(() {
_items.add(new Item(fileName, imageFile));
});
I'm trying to share the file using imageFile.path. I also tried including the image/png as mimeType, but still get the original file name and extension when I share the file.
UPDATE: I tried to find a solution for this bug, but nothing yet. It appears that is not a share_plus issue, image package neither. I printed the uri and mimeType in share_plus Kotlin code before start the activity and I got the right infos (png mimetype and the .png extension). I don't know how to proceed now.
My last (and unsuccessful) test was this:
final XFile? imageXFile = await _picker.pickImage(source: ImageSource.gallery);
final imageFile = File('${appDir.path}/$fileName.png');
imageFile.writeAsBytesSync(await imageXFile.readAsBytes());
Share.shareFiles([imageFile.path], mimeTypes: ["image/png"]);
I tried to pick a PNG image, save it and share it immediately, but still share the image as JPG.
OBS: While I was writing this update, I noticed that when I share with Telegram (as uncompressed file), it will be sent as JPG. When I share with Outlook, it is attached as PNG (with the name and extension that I want to).
Related
I have a PDF in the assets folder: assets/my_resume.pdf.
How can I open it in a PDF viewer? I don't want to open it in a widget. I want it to open outside of my app with whatever PDF viewer is available to this device.
I tried: https://pub.dev/packages/open_file but that doesn't work with assets.
Future<void> launchPdf() async {
}
How can I do this? I mostly care about mobile so not even considering web.
I don't know if this is the best method, but you could load the pdf file data, and save it to, for example, the Document directory on the device, and after that, use the open file package to open the pdf with a native device app.
void openAsset() async {
// load pdf into ram
ByteData pdf = await DefaultAssetBundle.of(context).load('assets/your_pdf.pdf');
// get documents directory
Directory documents = await getApplicationDocumentsDirectory();
// write data to a file in the documents directory
await File(documents.path + '/your_pdf.pdf').writeAsBytes(pdf.buffer.asUint8List());
// call open_pdf function
// ...
}
(Didn't test the code)
I want to upload an image from my assets folder into my firebase storage under a folder with the user id as the folder name. but is the first time on my life using firebase storage so I have no idea how to do that. I saw people only upload those images as "File" variable from the image picker, but I have no clew how to convert the image to File variable. Thank you.
I found that line of code:
FirebaseStorage.instance.ref(uid).child("profile-pic.png").putFile(/*what am i puting here?*/);
To get the files from the assets folder, then you must use the rootBundle, as in:
var fileName = "assets/demo.txt";
ByteData bytes = await rootBundle.load(fileName); //load sound from assets
Uint8List rawData = bytes.buffer.asUint8List(bytes.offsetInBytes, bytes.lengthInBytes);
FirebaseStorage.instance.ref('$fileName').putData(rawData);
Try that.
I'm working on a project that captures a screenshot of a widget and saves it in gallery.
my problem is there is few save to gallery packages in flutter and I tries almost all of them!
they save images as jpg which adds extra black bars around my widget which I don't want them to be there.
is there any package to save images to gallery in PNG format?
I've found the solution after 2 days of struggling with that.
you should save file as PNG to device path then use image_gallery_saver
package to save it as file
File('$dir/file_name${DateTime.now()}.png').writeAsBytes(pngBytes!);
final result = await ImageGallerySaver.saveFile(imagePath);
You can save image to gallery by using Image_gallery_saver plugin.image_gallery_saver
. For the black bars , you have to make sure that the screenshot is attached to the widget , what you want it image.
await ImageGallerySaver.saveImage(uint8list blob);
I'm working on a app where I would like to get the local path of the selected/picked image from my mobile gallery instead of image_picker path. I have used image _picker to pick an image from the gallery. When I print the path of the selected image it showing something like this /data/user/0/coffy.testapp/cache/image_picker5380473371212438250.jpg instead I need a path of the image like /data/emulated/downloads/021545.png (The path I can see in my mobile when I click the image details of a particular image);
You need to access local storage and select from there.
https://flutter.dev/docs/cookbook/persistence/reading-writing-files
https://pub.dev/packages/path_provider
I have implemented the possibility to take picture but what I want is to offer the user both options just like in other apps (e.g. WhatsApp) where you can choice between the Camera or Gallery.
I looked on the Image_picker documentation but didn't find anything. Am I missing something or there is no way to achieve it with this plugin?
I supposed you can create multiple different widgets (buttons) that access the different source of images? Not sure if it's the best way though.
Future getImage(ImageSource imageSource) async {
// ImageSource.camera or ImageSource.gallery
File image = await ImagePicker.pickImage(source: imageSource);
return image;
}