Actually I'm very new in Flutter-Dart.
And I'm writing an web app. So I should pick images from file path in web. And I should convert the image to base64 code. because I will add the image to my PHP server with my php codes. PHP code is ready. but I don't know a lot of information about pick images and convert base64. Can anybody help me.
Add file_picker: ^4.3.0 to pubspec.yaml and try this:
void pickFile(){
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
);
if (result != null) {
Uint8List? file = result.files.first.bytes!;
final x = base64Encode(file);
//do what you want with x.
}
}
Related
Question from a newbie in Flutter and Dart:
With this code I let a user to pick mulitple mp4/avi files in flutter web:
`FilePickerResult? picked = await FilePicker.platform
.pickFiles(
allowMultiple: true,
type: FileType.custom,
allowedExtensions: ['mp4', 'avi']);`
Now I would need an exression to extrcat the file name (not the path) of the selected files. I was hoping in something like this:
List<File> file_names = picked.files.name.toList();
But this is wrong.
Any suggestion?
You should try map function to extract the name property of each File object in the files list:
List<String> file_names = picked.files.map((file) => file.name).toList();
You can try like this to get List<File>
List<File> files = picked.paths.map((path) => File(path)).toList();
Also,FilePicker supports picking files from the web so it should not be issue.
And if you're planning to upload files to firebase then make sure you upload in Uint8List format.
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.
Package tried: https://pub.dev/packages/file_picker
Tried the example code implementation shared via GitHub. But the file path is returned as null for
web platform. Where same implementation in mobile platform return the path as expected.
https://github.com/miguelpruivo/flutter_file_picker/blob/master/example/lib/src/file_picker_demo.dart
Things aware of:
Path_Provider - Not supported for web
dart-io-library - Not supported for web
Open issue in Flutter Repo
Goal is to read the file from the path and not to upload. Any workaround to achieve this will be helpful.
Flutter channel: beta
As mentioned in the file_picker FAQ:
Paths aren't inaccessible from browsers since those provide fake paths. If you want to create a File instance to upload it somewhere, like FireStorage, you can do so with the bytes directly.
final result = await FilePicker.platform.pickFiles(type: FileType.any, allowMultiple: false);
if (result.files.first != null){
var fileBytes = result.files.first.bytes;
var fileName = result.files.first.name;
print(String.fromCharCodes(fileBytes));
}
I have a function for picking image from computer. Might work for you.
import 'dart:html';
void uploadImage({#required Function(File file) onSelected}) {
InputElement uploadInput = FileUploadInputElement()..accept = 'image/*';
uploadInput.click();
uploadInput.onChange.listen((event) {
final file = uploadInput.files.first;
final reader = FileReader();
reader.readAsDataUrl(file);
reader.onLoadEnd.listen((event) {
onSelected(file);
});
});
}
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>',
));
Using image_picker to select png files logs an error but the app works fine(the png file gets selected). The error i get
image_picker only supports compression for jpg files
I want to use it for .png files. Can I still use it or not?
Using file_picker package, https://pub.dev/packages/file_picker,(with filter, type: FileType.IMAGE) instead of image_picker package works with .png without logging an error.
I think that using the file_picker plugin would be the best option for this. It is a well developed plugin and easily implemented.
Here is an example implementation that you could use for PNG files only.
List<File> _paths;
FileType _pickingType;
bool _hasVailMime;
Future<List<File>> _openImageFileExplorer() async {
if(_pickingType != FileType.CUSTOM || _hasValidMime){
try {
_paths = await FilePicker.getMultiFile( // Or getFile
type: FileType.CUSTOM, fileExtension: 'png');
}
on PlatformException catch (e){
print("Unsupported operation: " + e.toString());
}
}
return _paths;
}