How to share image with share package in Flutter - flutter

Somewhere on stack i found an answer about how to share image trough share package (https://pub.dartlang.org/packages/share#-readme-tab-), and someone said to convert Bytes to base64 encoding and then share this string like that:
ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
String bs64 = base64Encode(pngBytes);
await Share.share(bs64).then((response){
Future.delayed(const Duration(milliseconds: 2000), () {
setState(() {
loadingShare = false;
loadingShareIndex = -1;
});
});
});`
I excpected to show an image but only shows long string to share...

Related

Save Image From ImagePicker Locally as a Memory(cache)

I want to save an Image from ImagePicker as a Memory but error Occured . Can you Please help me with this function and if another function needed to load image please Mentioned it below.
Uint8List? memoryImage;
Future getImage() async {
final picker = ImagePicker();
final image = await picker.getImage(source: ImageSource.camera);
if (image == null) return;
final Directory directory = await getApplicationDocumentsDirectory();
final path=directory.path;
final Filename=basename(image.path);
File file = File('$directory/$Filename.jpg');
final bytes = await file.readAsBytes();
final byte1= file.writeAsBytes(bytes);
setState(() {
memoryImage = byte1 as Uint8List?;
});
}
With this line you can write image bytes as a file.
File imageFile = await File(fileSavePath).writeAsBytes(imageBytes);
To access the Uint8List from the file you need to use
Uint8List memoryImage = File(imagePath).readAsBytesSync();
Or
Uint8List memoryImage = await File(imagePath).readAsBytes();
here the problem in your code is you are assigning file to a Uint8List. That's the error I guess

Flutter image_gallery_saver image not-showing after saving

I want to take a screenshot of my widget. For that, I am using the RepaintBoundary widget and to save the screenshot I use the image_gallery_saver plugin. But after saving the image it is not showing to the gallery. How to solve this issue?
class Utils {
static Future capture(GlobalKey key) async {
DateTime now;
now = DateTime.now();
if (key == null) return null;
final RenderRepaintBoundary boundary =
key.currentContext.findRenderObject();
final image = await boundary.toImage(pixelRatio: 3.0);
final byteData = await image.toByteData(format: ui.ImageByteFormat.png);
final pngByte = byteData.buffer.asUint8List();
// print(pngByte);
if (!(await Permission.storage.status.isGranted)) {
await Permission.storage.request();
}
final result = await ImageGallerySaver.saveImage(
Uint8List.fromList(pngByte),
quality: 90,
name:
'Screeshoot_${now.day.toString()}${now.hour.toString()}${now.minute.toString()}${now.second.toString()}');
return result;
// return pngByte;
}
}
I have done the same in my app but I have used the GallerySaver package instead.
First I have created a method that returns me the path where the image is located and then I use GallerySaver to save the image file.
Future<String> getImagePath() async {
RenderRepaintBoundary boundary = _renderQRandImageWidgetKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage(pixelRatio: 3.0);
ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
var pngBytes = byteData.buffer.asUint8List();
final tempDirectory = (await getTemporaryDirectory()).path;
final String filePath = '$tempDirectory/MyQRCode.png';
File imgFile = new File('$filePath');
await imgFile.writeAsBytes(pngBytes);
return filePath;
}
_saveImage(String userName) async {
try {
String filePath = await getImagePath();
bool isImageSaved = await GallerySaver.saveImage(filePath, albumName:"AlbumName");
SnackBar _snackbar = SnackBar(
content: isImageSaved ? Text('Image saved in gallery') : Text("There was an error while saving image"),
duration: const Duration(seconds: 1),
);
_scaffoldKey.currentState.showSnackBar(_snackbar);
} catch (exception) {
print("Error $exception");
SnackBar _snackbar = SnackBar(
content: Text('Something went wrong'),
duration: const Duration(seconds: 1),
);
_scaffoldKey.currentState.showSnackBar(_snackbar);
}

Invalid image on Creating thumbnails from video with flutter

Trying to generate an Thumbnail image from video , the file is created but , errors as Invalid image on load .Using this package video_thumbnail
Creating thumbnail ,
Future<File> genThumbnail(url) async {
//WidgetsFlutterBinding.ensureInitialized();
Uint8List bytes;
final Completer<ThumbnailResult> completer = Completer();
bytes = await VideoThumbnail.thumbnailData(
video: url,
imageFormat: ImageFormat.JPEG,
maxHeight: 250,
maxWidth: 300,
timeMs: 0,
quality: 0);
int _imageDataSize = bytes.length;
print("image size: $_imageDataSize");
//final _image = Image.memory(bytes);
//var _file =File.fromRawPath(bytes);
Directory tempDir = await getTemporaryDirectory();
var uint8list = bytes;
var buffer = uint8list.buffer;
ByteData byteData = ByteData.view(buffer);
File file = await File('${tempDir.path}/img/THUMBNAIL${DateTime.now().toIso8601String()}.JPEG').writeAsBytes(
buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
return file;
}
Saving to firestore
await genThumbnail(fileurl).then((_thumbFIle) async{
String Thumbfileurl = await uploadFile(_thumbFIle, 'thumbnailOf${filenamewithoutExtension}.JPEG', 'videothumbnail');
await sendFileToFirestoreChat(fileType, fileurl, filenamewithoutExtension,Thumbfileurl);
return fileurl;
});
The Saved Image ,
https://firebasestorage.googleapis.com/v0/b/proj-inhouse.appspot.com/o/videos%2Fvideothumbnails%2FthumbnailOfVID-20210301-WA0006.JPEG?alt=media&token=fa4f23c1-601f-486b-97d1-c63e221166af
Posting this as a Community Wiki as it's based on #pskink comments.
To resolve, add the writeAsBytes(bytes) instead of writeAsBytes(buffer.asUint8List()). There is no need for any buffer.

Flutter Converting network image into byteData

Is there any better way to convert network image to byteData? I am trying to convert network image from firebase url to byteData and here is my code:
Future<Uint8List> _loadNetworkImage() async {
try {
final response = await http.get(imageUrl);
return response.bodyBytes;
} catch (_) {
throw "Couldn't resolve network Image.";
}
}
Currently it takes almost 20+ seconds for a 7mb photo to be converted.
Here is how I ended up doing it.
Future<Uint8List?> _loadNetworkImage(String path) async {
final completer = Completer<ImageInfo>();
var img = NetworkImage(path);
img.resolve(const ImageConfiguration()).addListener(
ImageStreamListener((info, _) => completer.complete(info)));
final imageInfo = await completer.future;
final byteData =
await imageInfo.image.toByteData(format: ui.ImageByteFormat.png);
return byteData?.buffer.asUint8List();
}

Flutter: Accessing File: /storage/emulated/0/Android/data?

I'm making an app that takes a screenshot of the entire screen when clicking a button. Apparently the image goes to:
/storage/emulated/0/Android/data/com.example.program-name/files
I have tested this using the android emulator as well as my physical device. The path name changes slightly, but I still have no access to this image.
I am using the following code in order to take the screenshot and save it:
takeScreenShot() async{
RenderRepaintBoundary boundary = globalKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage();
final directory = (await getExternalStorageDirectory()).path;
print(directory);
ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
File imgFile = new File('$directory/screenshot.png');
print (imgFile.toString());
imgFile.writeAsBytes(pngBytes);
}
Is there anyway to:
Access this file and move it elsewhere on the computer/phone or,
Change the following directory to one that goes to any file on your computer?
final directory = (await getExternalStorageDirectory()).path;
The idea would be to save user data on a form and save an image of the page that was just filled in for future reference. i.e. submit form, save screenshot-image to PC desktop gallery, for example.
There are issues on this. #48208 #35783
On Android you can write path by yourself.
Future<void> _screenshot() async {
RenderRepaintBoundary boundary = globalKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage();
ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
Directory d = Directory('/storage/emulated/0');
if (d.existsSync()) {
Directory(d.path + '/MyApp').createSync();
File imgFile = new File(d.path + '/MyApp/screenshot.png');
print('saving to ${imgFile.path}');
imgFile.createSync();
imgFile.writeAsBytes(pngBytes);
}
}