How to Download Image from URL Instead Open in New Tab - flutter

I just want to download an image from the URL instead of open it in a new tab. I already try some code, but it didn't work as I want
1. url_launcher
launch(my_url);
2. url_launcher_web
final launcher = UrlLauncherPlugin();
launcher.launch(my_url);
3. dio
final response = await Dio().download(my_url, './xx.html');
UPDATE
Please note that I don't want to open the file in a new tab or current tab, I just want to direct download the file (something like making a stream to download).

For flutter web to download any file you can take the help of HTML library which will treat it as simply an anchor tag with download option. Note this can download any file so better check for IMAGE URL'S as well.
import 'dart:html' as html;
void downloadImage(String url){
html.AnchorElement anchorElement = new html.AnchorElement(href: url);
anchorElement.download = url;
anchorElement.click();
}

Related

Open PDF In PDF Viewer

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)

How to share files with a different extension

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).

How to share image + text both to WhatsApp from flutter app

I try to use this package
share_plus
from flutter to make user can share image + text with other apps (WhatsApp). It works fine on Android, but the problem is in IOS I can't share image and text same in one time. I searched for a long time but could not find a solution to this problem.
Code:
Future ShareImage()async{
var urls='https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/1200px-Image_created_with_a_mobile_phone.png';
final url=Uri.parse(urls);
final res =await http.get(url);
final bytes =res.bodyBytes;
final temp = await getTemporaryDirectory();
final path ='${temp.path}/imageToShare.jpg';
File(path).writeAsBytesSync(bytes);
Share.shareFiles([path],text:'Great picture'); }
Anyone have a way to solve this problem?
I think you need these packages for your need according.
whatsapp_share2: https://pub.dev/packages/whatsapp_share2
whatsapp_share: https://pub.dev/packages/whatsapp_share
akvelon_flutter_share_plugin:
https://pub.dev/packages/akvelon_flutter_share_plugin
flutter_share: https://pub.dev/packages/flutter_share
More package available at https://pub.dev

Flutter : How to block full screen video in webview?

I use "webview_flutter: ^1.0.7" plugin in my flutter app to open a webview but I have a problem when a website launch a video after a click anywhere on the screen an it's open in fullscreen. How to disable it without disable javascript ? I want to play the video but not in fullscreen
I can't use "AutoMediaPlaybackPolicy" because there is an action
One way would be to update html file:
String hmtlCode = (await http.get(initialUrl)).body;
# edit this code (either append to string, or find and replace)
https://itqna.net/questions/80033/how-disable-full-screen-html5-video-any-browser (html example how to disable full screen)
and then feed this html to webview
String hmtlCode = ....;
String url = Uri.dataFromString(hmtlCode, mimeType: 'text/html', encoding: Encoding.getByName('utf-8')).toString();
WebView(
initialUrl: url,
...

Flutter Web Save Image

I was working on a flutter web app where I need to save an image created using canvas.I tried FileSaver.js library but it wasn't successful for me.
Index.html main.dart
I created simple package image_downloader_web that can easily download image from web.
final WebImageDownloader _webImageDownloader = WebImageDownloader();
const _url = "https://picsum.photos/200";
Future<void> _downloadImage() async {
await _webImageDownloader.downloadImageFromWeb(_url);
}