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.
Related
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
I am using image picker to pick image from device(App) and after that stored that file in provider class like that.
File? _img;
get img=> _img;
void putbannerimg(File img) {
_img = img;
notifyListeners();
}
I found out that image picker does not compress png images, I tried compressing it with flutter_image_compress
compressFile() async {
final formservice = Provider.of<PostForm>(context, listen: false);
File file = formservice.bannerfile;
final result = await FlutterImageCompress.compressWithFile(
file.absolute.path,
quality: 54,
);
formservice.putbannerimg(File.fromRawPath(result!));
}
I tried this way And other multiple ways but getting different different errors I want to upload this file in firebase storage like this
var task = storageicon.putFile(formservice.iconfile);
please tell me where I am going wrong, without compress file all things are working fine
Edit: I found out that path should be a string how can I parse local code file in that
If you are using the "flutter_image_compress" package
You may have to use the compress function for files stored on the phone like this:
Future<File> testCompressAndGetFile(File file, String targetPath) async {
var result = await FlutterImageCompress.compressAndGetFile(
file.absolute.path, targetPath,
quality: 88,
rotate: 180,
);
print(file.lengthSync());
print(result.lengthSync());
return result;
}
"compressAndGetFile()" returns a file while "compressWithFile()" returns a Uint8List.
If i understand your last question right, you want to use an image built into your application by default.
For this, you have to open your "pubsepc.yaml", go to the "flutter:" mark and add "assets:" like so:
flutter:
assets:
- path
"path", in my example, describes a top level folder containing assets like pictures.
You can freely describe its name.
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');
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();
}
Looking for a good sample working code for downloading any file from URL in flutter. If its with native downloader then this will be very good. Please help me with sample of code to download any file using native downloader in flutter.
I have used few libraries but didn't turned out well.
For a mobile device, I used the http package to download a file to the applications document directory
First, create a httpClient object
static var httpClient = new HttpClient();
Then you can create a function like this to download the file:
Future<void> _downloadFile({
required String fileName,
}) async {
String url = ...;
var request = await httpClient.getUrl(Uri.parse(url));
var response = await request.close();
var bytes = await consolidateHttpClientResponseBytes(response);
String dir = (await getApplicationDocumentsDirectory())!.path;
File file = new File('$dir/$fileName'); // Note: Filename must contain the extension of the file too, like pdf, jpg etc.
await file.writeAsBytes(bytes);
}
For a flutter web application, I felt the url_launcher package was the easiest to work with.
_launchURL() async {
String url = ...;
if (await canLaunch(url)) {
await launch(url);
print('URL Launcher success');
} else {
throw Exception('Could not launch $url');
}
}
The url_launcher package code works even for a mobile device but it opens a new browser window to download the required file which is not a good user experience so I have used 2 approaches for the same problem.
Hope I have answered your query.