How to limit wechat asset picker to only JPG imges - flutter

In my app I want to allow users to pick multiple photos, I am usin for this Wechat Asset Picker, as I need to put a limit nfor number of photos can be selected.
afer that I need to compress these photos to reduce their siz befor uploadting to firestore.
I have two problems:
first, we chat asset picker package show all assets on the device, images, videos, etc. while I need to alow user only to upload images.
second, I am using Flutter Image Compress package for compressing, which works only with JPG.
Is there any better packages for my objective?
if not, my questions is how to limit wechat asset picker to only jpg images?
snapshots of code:
final List<myimagepicker.AssetEntity>? result =
await myimagepicker.AssetPicker.pickAssets(
context,
pickerConfig: const myimagepicker.AssetPickerConfig(
gridCount: 3,
maxAssets: 10,
pickerTheme: null,
themeColor: Color.fromARGB(255, 245, 91, 165),
pageSize: 30,
),
var result = await FlutterImageCompress.compressAndGetFile(
imageslist[i].absolute.path,
outPath,
quality: 50,
);

resultList = await AssetPicker.pickAssets(
context,
pickerConfig: const AssetPickerConfig(
maxAssets: 3,
requestType: RequestType.image, ✅
),
only jpg?
don't know
but only image?
you can set requestType.

Related

Flutter display package:image/image.dart:Image in UI Widget

We can work on images in Flutter using import 'package:image/image.dart' as ToolsImage; package to scale them with ToolsImage.copyResize() and manipulate individual pixels with ToolsImage.Image.getPixel(x,y)
To display image from memory bytes (ByteData/Uint8List/List<int>) we can use MemoryImage:
Container(child: Image(image: MemoryImage(image.data.buffer.asUint8List())))
I'm getting an exception:
Exception: Invalid image data
whichever List I use. What's wrong?
Remember to add HEADER to image bytes after format conversions!
ByteData imageByteData = await rootBundle.load('res/images/basic/dizzy-face.png');
List<int> values = imageByteData.buffer.asUint8List();
ToolsImage.Image? photo = ToolsImage.decodeImage(values);
photo = ToolsImage.copyResize(photo!, height: 32, width: 32);
removes header information (pixel-by-pixel manipulation removes header info).
Use:
List<int>? imageWithHeader = ToolsImage.encodeNamedImage(TEST_IMAGE, ".bmp");
and then:
Container(child: Image(image: MemoryImage(Uint8List.fromList(imageWithHeader))))
Last line enables us to convert package:image/image.dart to package:flutter/src/widgets/image.dart (UI Image Widget)

Image not updated after changing it's File Path in Flutter

By facing image quality not decreased after compressing it multiples time I thought that plugin is not working. But after dumping file on File Storage during debugging, It was Actually Compressed and Image Quality was decreased by 25 %.
But Image is not Updating after setting it's State. I've read many articles and things. I've used all the things I've Found. I tried to clear Flutter's Image Cache and Painting Binding and Stuff. I also tried to empty temporary directory.
imageCache.clearLiveImages();
PaintingBinding.instance.imageCache.clear();
var appDir = (await getTemporaryDirectory()).path;
Directory(appDir).delete(recursive: true);
Here's My Code. First _imagePath is the image it receives and it's displayed properly. When we compress and update the File Path, Image's State is not changed (Image is not changed).
CircleAvatar(
radius: 150,
backgroundImage: FileImage(File(_imagePath)),
),
TextButton(
child: Text(
'Compress',
style: TextStyle(fontSize: 22),
),
onPressed: () {
setState(() async {
// Compressing File
File compressedFile =
await FlutterNativeImage.compressImage(
_imagePath,
quality: 25,
);
_imagePath = compressedFile.path;
});
},
),
I used Value Notifier of flutter which notifies if value changes for image.

How to compress image asset from multi_image_picker flutter?

Libraries used: https://pub.dev/packages/multi_image_picker, https://pub.dev/packages/flutter_image_compress
I am using multi image picker lib to get multiple images from the gallery. However, before uploading them I want to compress image's size first.
Multiple image picker return List<Asset> but in flutter_image_compress lib, we can only compress image as a type of File like this:
Future<Uint8List> testCompressFile(File file) async {
var result = await FlutterImageCompress.compressWithFile(
file.absolute.path,
minWidth: 2300,
minHeight: 1500,
quality: 94,
rotate: 90,
);
return result;
}
How can I convert List<Asset> to List<File> in order to compress image?
I solved this problem. Please check my full source code here: https://soksereyphon8.medium.com/upload-multiple-images-and-compress-image-in-flutter-62d113a3247a

Saving a Video into gallery using path_provider package

I have been Working on a small project where users can download WhatsApp status directly into their gallery. I made two tabs. one tab for WhatsApp Images and another for Videos. I could successfully download images into the gallery. now, I am facing a problem while downloading a Video into the gallery. I used path provider to download the video into the gallery. the Video gets downloaded in storage\emulated\0\android\data\com.JeevanCrasta.stikkers\files\videos\
I want to download those videos in the storage\emulated\0\Stikkr\videosdirectory. And those videos should be visible in the gallery. now those videos get downloaded in a different directory and are not visible in the gallary.
this is the code where videos get downloaded while the button is pressed :
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.teal,
child: Icon(Icons.download_sharp),
onPressed: () async {
_onLoading(true, "");
File originalVideoFile = File(widget.videoFile);
Directory directory = await getExternalStorageDirectory();
if (Directory("${directory.path.toString()}/Stikkr").existsSync()) {
Directory("${directory.path.toString()}/Stikkr")
.createSync(recursive: true);
}
String path = directory.path;
String curDate = DateTime.now().toString();
String newFileName = "$path/Stikkr/Videos/VIDEO-$curDate.mp4";
// print(newFileName);
await originalVideoFile.copy(newFileName);
_onLoading(false,
"If Video not available in gallary\n\nYou can find all videos at");
},
),
You can use package https://pub.dev/packages/gallery_saver
1 Saves images and videos from network, it has to contain http/https prefix.
2 Save temporary file to external storage
Both images and videos will be visible in Android Gallery and iOS Photos.
In your case, file has downloaded, so you can pass newFileName and use
GallerySaver.saveVideo(newFileName).then((String path) {
setState(() {
secondButtonText = 'video saved!';
});
});

Flutter Resize ImagePicker Image before uploading to firebase

I am building a demo wallpaper app using flutter where users can upload images to firebase. When loading those images I first want to load a small version of the image and only once the user clicks on the image, load the full version.
In order to achieve this I thought I would simply upload 2 versions in the background once a user picks the image. Now I am struggling with how to achieve this.
Here is how the user picks the image using ImagePicker into a file var.
Future pickImage() async {
var tempImage = await ImagePicker.pickImage(source: ImageSource.gallery, maxHeight: 2000);
print(tempImage.runtimeType);
setState(() {
inspirationimage = tempImage;
});
String result = await uploadImage();
}
As you can see the tempimage is the full size version. I would now have sth like this:
var smallImage = tempImage.resize(height: 200);
Obviously this does not work as tempImage is of type file. Any ideas how this is usually solved?
Thanks
Since you are using Firebase, you can use the Firebase "Resize Image" extension. If you want to do it on client side however, take a look at this answer.
Why don't you let your user crop the image by using this
image_cropper 1.3.1 plugin?
link : https://pub.dev/packages/image_cropper
Example
// File image = await ImagePicker.pickImage(source: source);
final _picker = ImagePicker();
PickedFile image = await _picker.getImage(source: source);
if (image != null) {
// Remove crop attribute if we don't want to resize the image
File cropped = await ImageCropper.cropImage(
sourcePath: image.path,
aspectRatio: CropAspectRatio(ratioX: 1, ratioY: 1),
compressQuality: 100, // 100 means no compression
maxWidth: 700,
maxHeight: 700,
compressFormat: ImageCompressFormat.jpg,
androidUiSettings: AndroidUiSettings(
toolbarColor: primaryColor,
toolbarTitle: "RPS Cropper",
statusBarColor: primaryColor,
backgroundColor: Colors.white,
//toolbarWidgetColor: primaryColor,
activeControlsWidgetColor: primaryColor,
//dimmedLayerColor: primaryColor,
cropFrameColor: primaryColor,
cropGridColor: primaryColor,
),
);