Flutter 如何通过分享插件分享文件图片
Flutter How to share File image via share plug in
Hello and thank you in advance!
我正在从事图像编辑工作。我可以从图像选择器中选择图像并在 drawstringdrawstring 的帮助下重写它。我可以将图像保存在我的文档目录中。我想通过共享插件从 documentDirectory 路径共享我的图像。但是,我收到一个错误。
代码和错误详情如下所示。
代码:
FlatButton(
child: Text("Apply Watermark Over Image"),
onPressed: () async {
final image1 = ui.decodeImage(_originalImage.readAsBytesSync());
ui.drawString(image1, ui.arial_24, 300, 400, 'Hello And thank you');
final documentDirectory = await getApplicationDocumentsDirectory();
final file = new File(p.join(documentDirectory.path, "merged_image.jpg"));
file.writeAsBytesSync(ui.encodeJpg(image1));
// I HAVE THE IMAGE IN - documentDirectory.path "HERE I CANT SHARE THAT IMAGE"
final ByteData bytes = await rootBundle.load(documentDirectory.path);
await Share.file('esys image', 'esys.png', bytes.buffer.asUint8List(), 'image/png',
text: 'My optional text.');
},
)
我收到这个错误:
[错误:flutter/lib/ui/ui_dart_state.cc(177)] 未处理的异常:无法加载资产:/data/user/0/com.photogranth.watermark/app_flutter
对于共享文件图像,您可以使用此 flutter_share_file
FlatButton(
onPressed: () async {
Directory dir = await getApplicationDocumentsDirectory();
File testFile = new File("${dir.path}/image.png");
FlutterShareFile.shareImage(dir.path, "image.png", ShareFileType.image);
},
)
我们可以使用share package来分享文件,这个插件支持分享图片,文字和主题
这是完整的例子
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:share/share.dart';
List<String> imagePaths = [];
_AddFile(int position) {
final imagePicker = ImagePicker();
final pickedFile = await imagePicker.getImage(
source: ImageSource.gallery,
);
if (pickedFile != null) {
setState(() {
imagePaths.add(pickedFile.path);
});
}
},
_onShare(BuildContext context) async {
final RenderBox box = context.findRenderObject() as RenderBox;
if (imagePaths.isNotEmpty) {
await Share.shareFiles(imagePaths,
text: text,
subject: subject,
sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);
} else {
await Share.share(text,
subject: subject,
sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);
}
}
Hello and thank you in advance!
我正在从事图像编辑工作。我可以从图像选择器中选择图像并在 drawstringdrawstring 的帮助下重写它。我可以将图像保存在我的文档目录中。我想通过共享插件从 documentDirectory 路径共享我的图像。但是,我收到一个错误。
代码和错误详情如下所示。
代码:
FlatButton(
child: Text("Apply Watermark Over Image"),
onPressed: () async {
final image1 = ui.decodeImage(_originalImage.readAsBytesSync());
ui.drawString(image1, ui.arial_24, 300, 400, 'Hello And thank you');
final documentDirectory = await getApplicationDocumentsDirectory();
final file = new File(p.join(documentDirectory.path, "merged_image.jpg"));
file.writeAsBytesSync(ui.encodeJpg(image1));
// I HAVE THE IMAGE IN - documentDirectory.path "HERE I CANT SHARE THAT IMAGE"
final ByteData bytes = await rootBundle.load(documentDirectory.path);
await Share.file('esys image', 'esys.png', bytes.buffer.asUint8List(), 'image/png',
text: 'My optional text.');
},
)
我收到这个错误:
[错误:flutter/lib/ui/ui_dart_state.cc(177)] 未处理的异常:无法加载资产:/data/user/0/com.photogranth.watermark/app_flutter
对于共享文件图像,您可以使用此 flutter_share_file
FlatButton(
onPressed: () async {
Directory dir = await getApplicationDocumentsDirectory();
File testFile = new File("${dir.path}/image.png");
FlutterShareFile.shareImage(dir.path, "image.png", ShareFileType.image);
},
)
我们可以使用share package来分享文件,这个插件支持分享图片,文字和主题
这是完整的例子
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:share/share.dart';
List<String> imagePaths = [];
_AddFile(int position) {
final imagePicker = ImagePicker();
final pickedFile = await imagePicker.getImage(
source: ImageSource.gallery,
);
if (pickedFile != null) {
setState(() {
imagePaths.add(pickedFile.path);
});
}
},
_onShare(BuildContext context) async {
final RenderBox box = context.findRenderObject() as RenderBox;
if (imagePaths.isNotEmpty) {
await Share.shareFiles(imagePaths,
text: text,
subject: subject,
sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);
} else {
await Share.share(text,
subject: subject,
sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);
}
}