share_plus does not share the text flutter - flutter

I am trying to implement sharing widget as a screenshot via share_plus.dart plug in. It works, however it does not attach the string text to the widget. How do I fix that?
Future<void> shareReview() async {
final image = await controller.capture();
saveAndShare(image);
}
void saveAndShare(Uint8List bytes) async {
final directory = await getApplicationDocumentsDirectory();
final image = File('${directory.path}/review.png');
image.writeAsBytesSync(bytes);
final text = 'blahblahblahbalh';
await Share.shareFiles([image.path], text: text);
}

Related

Can't able to save and display camera images in gridView in flutter using image_picker

I am creating a widget in which I want to display a gridview of image clicked through mobile camera at runtime. I had used image_picker package from flutter. But it is only working for picking image from gallery and everytime I click image it crashes.
class _ImageInputState extends State<ImageInput>
with AutomaticKeepAliveClientMixin {
// list of images
final List<File> _imageFileList = [];
final ImagePicker _picker = ImagePicker();
Function for picking image from camera using image_picker
final imageFile = await _picker.pickImage(source: imageSource);
if (imageFile == null) return;
File tmpFile = File(imageFile.path);
final appDir = await getApplicationDocumentsDirectory();
final fileName = basename(imageFile.path);
final localFile = await tmpFile.copy('${appDir.path}/$fileName');
// setState(() {
// _imageFileList.add(localFile);
//
// });
_pickImageGall(ImageSource.gallery);
}
function for picking images from the gallery of phone
Future<void> _pickImageGall(ImageSource imageSource) async {
final _pickedImage = await _picker.pickImage(source: imageSource);
if (_pickedImage != null) {
setState(() {
_imageFileList.add(File(_pickedImage.path));
});
}
}
and in the ui there is a gridview to display the images in which the last index is a input widget where there is two IconButton one for camera input and other for gallery input. As I am making this thing for the first time I am confused how to procedure will be . I had try to implement every solution from the stackoverflow but none worked for me. Please give the solution.

Save ImagePicker Image in Shared Preferences - Flutter

I'm trying to save image picked from ImagePicker and store it in shared preferences and then retrieve it from there but no luck so far
To make my question more specific, how to save an image as a string in shared preference and then later retrieve it
Here is my code
File? profileImage;
void saveData(String key, String value) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(key, value);
}
void getData(String key) async {
final prefs = await SharedPreferences.getInstance();
final image = await prefs.getString(key);
setState(() {
profileImage = image; //this would result into error because profileImage expect file type value
});
}
Future pickProfile() async {
final profileImagePicker = await ImagePicker().pickImage(source: ImageSource.gallery);
final File profile = File(profileImagePicker!.path);
final directoryPath = await getApplicationDocumentsDirectory();
final path = directoryPath.path;
final imageFile = await File(profileImagePicker.path).copy('$path/image1.png'); // What am I supposed to do after this step
saveData('profile', path); what value needs to be stored here, it expects a string
setState(() {
profileImage = profile;
});
}
To convert image into String you can use below code
final bytes = imageFile.readAsBytesSync();
String imageString = base64Encode(bytes);
To convert String to Image
Uint8List bytes = BASE64.decode(base64ImageString);
You can use the Image widget to diplay the Image
Image.memory(bytes);

How do I combine Text and an Image File into one Image File in Flutter?

Below is a snippet of code from a function that uploads a generated QR code (using the qr_flutter package) to firebase storage; then gets the firebase storage url to save in a custom model that is uploaded to firebase firestore (not shown).
This works fine, however I want to upload a file that consists of the QR code bounded by title text above and address text below. (Essentially a Column with children [title, qrFile, address]).
My question is: How do I combine Text and my qrFile into a single image file that I can upload to firebase storage?
String qrString = 'qr_data_here';
final qrValidationResult = QrValidator.validate(
data: qrString,
version: QrVersions.auto,
errorCorrectionLevel: QrErrorCorrectLevel.L,
);
if (qrValidationResult.status == QrValidationStatus.valid) {
final qrCode = qrValidationResult.qrCode;
const String title = 'title_name_here';
final String address = 'address_here';
final painter = QrPainter.withQr(
qr: qrCode!,
color: const Color(0xFF000000),
gapless: true,
embeddedImageStyle: null,
embeddedImage: null,
);
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
final ts = DateTime.now().millisecondsSinceEpoch.toString();
String path = '$tempPath/$ts.png';
// ui is from import 'dart:ui' as ui;
final picData =
await painter.toImageData(2048, format: ui.ImageByteFormat.png);
// writeToFile is seen in code snippet below
await writeToFile(
picData!,
path,
);
} else {
genericErrorDialog(context);
}
// qrStorage is a reference to a folder in firebase storage
await qrStorage.child('name_here').putFile(qrFile);
var url =
await qrStorage.child('name_here').getDownloadURL();
late File qrFile;
Future<void> writeToFile(ByteData data, String path) async {
final buffer = data.buffer;
qrFile = await File(path).writeAsBytes(
buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
}
One solution is to use the screenshot package (https://pub.dev/packages/screenshot). This package has a function to save a widget as an image (without displaying it on screen) as shown below.
ScreenshotController screenshotController = ScreenshotController();
await screenshotController
.captureFromWidget(CustomWidget())
.then((capturedImage) async {
await do_something_with_capturedImage_here();
});
As it relates to my question specifically; Below is the code to generate a qr code, place it in a widget (needs some more formatting) with text, and then save the widget as an image file and upload to firebase.
String qrString = 'qr_data_here';
final qrValidationResult = QrValidator.validate(
data: qrString,
version: QrVersions.auto,
errorCorrectionLevel: QrErrorCorrectLevel.L,
);
if (qrValidationResult.status == QrValidationStatus.valid) {
final qrCode = qrValidationResult.qrCode;
const String title = 'title_name_here';
final String address = 'address_here';
final painter = QrPainter.withQr(
qr: qrCode!,
color: const Color(0xFF000000),
gapless: true,
embeddedImageStyle: null,
embeddedImage: null,
);
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
final ts = DateTime.now().millisecondsSinceEpoch.toString();
String path = '$tempPath/$ts.png';
// ui is from import 'dart:ui' as ui;
final picData =
await painter.toImageData(2048, format: ui.ImageByteFormat.png);
// writeToFile is seen in code snippet below
await writeToFile(
picData!,
path,
);
await screenshotController
.captureFromWidget(Column(
children: [
Text(title),
Image.file(qrFile),
Text(address),
],
))
.then((capturedImage) async {
await widgetToImageFile(capturedImage);
});
} else {
genericErrorDialog(context);
}
// qrStorage is a reference to a folder in firebase storage
await qrStorage.child('name_here').putFile(fullQrFile);
var url =
await qrStorage.child('name_here').getDownloadURL();
ScreenshotController screenshotController = ScreenshotController();
late File qrFile;
late File fullQrFile;
Future<void> writeToFile(ByteData data, String path) async {
final buffer = data.buffer;
qrFile = await File(path).writeAsBytes(
buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
}
Future<void> widgetToImageFile(
Uint8List capturedImage,
) async {
Directory newTempDir = await getTemporaryDirectory();
String newTempPath = newTempDir.path;
final newTs = DateTime.now().millisecondsSinceEpoch.toString();
String path = '$newTempPath/$newTs.png';
fullQrFile = await File(path).writeAsBytes(capturedImage);
}

Flutter share image - open failed: ENOENT (No such file or directory), null, null)

https://pub.dev/packages/share
Dependency:
share: ^0.6.5+2
Local file directory
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
Share function
Future<void> _onShare(BuildContext context) async {
final RenderBox box = context.findRenderObject();
final path = await _localPath;
await Share.shareFiles(
['$path/assets/images/${widget.imgUrl}.png'],
text: text,
sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);
}
Button - on press
FlatButton(child: Text('Share'), onPressed: () => _onShare(context)),
I'm using this package to share image from application. I have no issue sharing text but when I add the function to share image, I keep getting errors for missing image.
Am I doing it correctly?
Managed to figure out the problem.
Seems like I need to save image before I can reference the image.
final ByteData bytes = await rootBundle
.load('assets/images/${widget.imgUrl}.png');
final Uint8List list = bytes.buffer.asUint8List();
final directory = (await getExternalStorageDirectory()).path;
File imgFile = File('$directory/screenshot.png');
imgFile.writeAsBytesSync(list);
Share.shareFiles(['$directory/screenshot.png'],
sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);

Flutter save Image in Gallery folder

I am currently working on an alert box with the option to take a photo or select a picture from the gallery.
The status so far is:
I can get pictures from the gallery with the ImagePicker, this works perfectly.
Now I come to my problem:
Saving the captured images also works, but it is saved in the storage and therefore not displayed in the gallery. Please help me
Image AlertBox: https://imgur.com/a/IhZ5Sgh
Image Empty rencent folder
https://imgur.com/a/W3FvPtS
Made pictures: https://imgur.com/a/VIZOTBH
Here is the path where the image is stored:
File: '/storage/emulated/0/Android/data/com.example.supportanfrage/files/Pictures/1cd284f4-3632-4ed8-8c6a-14d7be83a8335698897692938961258.jpg'
Methode for saving images
Future getAndSaveImage() async {
final File image = await ImagePicker.pickImage(source: ImageSource.camera);
debugPrint(image.toString());
if (image == null) return;
final directory = await getExternalStorageDirectory();
final String path = directory.path;
this._fileName = path;
final File localImage = await image.copy(path);
}
I using the following dependencies / plugins:
file_picker: ^1.3.8
camera: ^0.5.2+2
image_picker: ^0.6.0+17
image_gallery_saver: ^1.1.0
path_provider: ^1.1.2
Thanks.
This is the sample source code provided for the flutter camera plugin. It will take timeStamp and then save your photo with the name of that timeStampValue.jpg in your phone storage.
Future<String> takePicture() async {
if (!controller.value.isInitialized) {
return null;
}
String timestamp() => DateTime.now().millisecondsSinceEpoch.toString();
final Directory extDir = await getApplicationDocumentsDirectory();
final String dirPath = '${extDir.path}/Pictures/flutter_test';
await Directory(dirPath).create(recursive: true);
final String filePath = '$dirPath/${timestamp()}.jpg';
if (controller.value.isTakingPicture) {
return null;
}
try {
await controller.takePicture(filePath);
} on CameraException catch (e) {
return null;
}
return filePath;
}