How to Share Image from API(URL) in flutter? - 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');

Related

How to view online 3d assets in flutter ARKit

I'm working on an AR app that fetches 3d assets from a django server and display them using arkit_plugin for flutter.
first I tried passing the link of the model as a url directly, but this didn't work. it showed nothing
node = ARKitReferenceNode(
url: "http://localhost:8000/models/ast.dae",
scale: vector.Vector3.all(0.3),
);
controller.add(node, parentNodeName: anchor.nodeName);
then I tried downloading the model to the documents folder of my iPhone
Future fetchFile(url, fileName) async {
// code for downloading models
final appStorage = await getApplicationDocumentsDirectory();
final file = File(appStorage.path);
final responce = await Dio().get(
url,
options: Options(
responseType: ResponseType.bytes,
followRedirects: false,
receiveTimeout: 0
)
);
final raf = file.openSync(mode: FileMode.write);
raf.writeFromSync(responce.data);
await raf.close();
return file;
}
after that i passed the path of the downloaded model as a url
final appStorage = await getApplicationDocumentsDirectory();
node = ARKitReferenceNode(
url: "${appStorage.path}/modelname.dae",
scale: vector.Vector3.all(0.3),
);
controller.add(node, parentNodeName: anchor.nodeName);
but still didn't work
I also tried loading models from my assets folder of my flutter project, but it still didn't work.
image of my xcode project folder
but after I placed my models in the models.scnassets folder under the Runner folder and passed models.scnassets/dash.dae as a url, it worked, but the problem is I can't view online models in my app. Is there a way to download the models to the models.scnassets folder from the flutter app or other way to solve this problem?
Thanks in advance

flutter ImageGallerySaver ios gif download error

I'm using ImageGallerySaver for downloading gif files.
At Android platform, It takes good.
But iOS platform, gif files save as jpg. So, files not animated...
please help me.
My code likes this.
var appDocDir = await getTemporaryDirectory();
String savePath = appDocDir.path + "/temp.gif";
await Dio().download(imgUrl, savePath);
final result = await ImageGallerySaver.saveFile(savePath);

Flutter imagepicker.pickvideo return jpg

I'm using this to return the video file but I got .jpg
Future<File> getVideo() async {
var video = await ImagePicker.pickVideo(
source: ImageSource.gallery);
return video;
}
I want to ImagePicker.pickVideo() return video file instead of .jpg file so I can upload this file to firebase, how can I achieve that?
I'm assuming that you're using the package:
https://pub.dev/packages/image_picker
pickVideo() method has been decrecated, and you will need to replace these apis with getVideo()
As explained the repositories' documentation:
https://github.com/flutter/plugins/tree/master/packages/image_picker/image_picker
Write this:
final _picker = ImagePicker();
PickedFile video = await _picker.getVideo(...)
However I would suggest to use this package as an alternative:
https://pub.dev/packages/flutter_document_picker
This package will allow you to select all videos on the device, including those taken from a users' Google Drive or iCloud providers. In this case write this:
FlutterDocumentPickerParams params = FlutterDocumentPickerParams(
allowedUtiTypes: [
'public.video',
'public.mpeg',
'public.mpeg-4-audio',
'com.apple.protected-​mpeg-4-audio'
],
allowedMimeTypes: [
'video/mpeg',
'video/x-flv',
'video/mp4',
'application/x-mpegURL',
'video/quicktime',
'video/x-msvideo',
'video/x-ms-wmv',
'video/ogg',
'video/mp2t',
'video/3gpp'
],
invalidFileNameSymbols: ['/'],
);
return await FlutterDocumentPicker.openDocument(params: params);
You will need to make sure that the Mimes and Uti types for videos on iOS & Android are set correctly.

Store image uploaded by user into Flutter Web as an actual .jpg file

I am using the flutter_web_image_picker package to allow the user to select -and then upload to Firebase- an image.
However, the package returns an image widget, which I can display, but I cannot upload to Firebase. Therefore, I am trying to read the package's code and update it to fit my needs.
In general, I think the packages main functionalities are:
It gets the file
//...
final reader = html.FileReader();
reader.readAsDataUrl(input.files[0]);
await reader.onLoad.first;
final encoded = reader.result as String;
Then it 'strippes' it
final stripped = encoded.replaceFirst(RegExp(r'data:image/[^;]+;base64,'), '');
final imageName = input.files?.first?.name;
//...
To finally return it as a Widget:
final imageName = imageName;
final imageData = base64.decode(stripped);
return Image.memory(imageData, semanticLabel: imageName);
As I said, it works perfectly, however, I need to adapt it to my needs:
I would like to get the image as a .jpg file so that I can upload it to Firebase.
Is any of the variables above the actual .jpg file? Is there any transformation that I should perform to get a .jpg file?
Thanks!
I based my answer on this post.
Basically, on the flutter_web_image_picker package, before the code I posted, there were a few lines that get an actual html file:
final html.FileUploadInputElement input = html.FileUploadInputElement();
input..accept = 'image/*';
input.click();
await input.onChange.first;
if (input.files.isEmpty) return null;
Then using firebase's pacakge, I uploaded the image as follow:
import 'package:firebase/firebase.dart' as fb;
fb.StorageReference storageRef = fb.storage().ref('myLocation/filename.jpg');
fb.UploadTaskSnapshot uploadTaskSnapshot = await storageRef.put(input.files[0]).future;
Uri imageUri = await uploadTaskSnapshot.ref.getDownloadURL();
return imageUri;

How to convert a PDF file to an image with Flutter?

How can I convert a PDF file into an image with Flutter?
I want to print the image to an ESC/POS printer using esc_pos_printer. This package won't accept PDFImage, it needs to be a Flutter Image.
I see plenty of PHP plugins that do this but nothing for Flutter.
edit: There is an answer to another question here which shows some code to decode an image from "pdf64" but I can't figure out exactly what "pdf64" is.
I created a PDF from html using flutter_html_to_pdflike this:
Directory appDocDir = await getApplicationDocumentsDirectory();
var targetPath = appDocDir.path;
var generatedPdfFile = await FlutterHtmlToPdf.convertFromHtmlContent(
htmlContent, targetPath, targetFileName);
generatedPdfFilePath = generatedPdfFile.path;
Now I need to know how to create a Flutter Image from that PDF or the bytecode to send raw to the printer.
You can use https://pub.dev/packages/printing:
await for (var page in Printing.raster(document)) {
final image = page.asImage();
...
}
This plugin can also convert your Html to Pdf with this:
final pdf = await Printing.convertHtml(
format: PdfPageFormat.a4,
html: '<html><body><p>Hello!</p></body></html>',
));