Tim*_*rks 5 share dart flutter
我想分享一张从 CameraController 拍摄的图像。
我的文件位置例如 /data/user/0/com.user.test/cache/2019-09-10 16:32:52.281842.png
如何共享这个本地图像?
我添加了这两行用于读/写本地存储:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Run Code Online (Sandbox Code Playgroud)
我使用https://pub.dev/packages/esys_flutter_share中的共享组件,效果很好。
void _sharePicture() async {
print('Share picture');
print(this.imagePath);
final ByteData bytes = await rootBundle.load(this.imagePath);
await Share.file('esys image', 'esys.png', bytes.buffer.asUint8List(), 'image/png', text: 'My optional text.');
}
Run Code Online (Sandbox Code Playgroud)
this.imagePath 是文件的本地位置: :/data/user/0/com.user.test/cache/2019-09-10 16:32:52.281842.png
您首先必须保存图像吗?并用它来分享?怎么可能分享这个本地图片呢?
这个想法是共享 Uint8List
该演示使用camera_camera包的示例。https://github.com/gabuldev/camera_camera/tree/master/examplecamera_camera
包https://pub.dev/packages/camera_camera是一个很棒的包,具有制作精良的功能,并在内部使用相机插件
代码片段
点击拍照后,系统返回一个文件(本例中为val),读取字节并传输到Uint8List
print("path ${val}");
List<int> bytes = await val.readAsBytes();
Uint8List ubytes = Uint8List.fromList(bytes);
await Share.file('ESYS AMLOG', 'amlog.jpg', ubytes, 'image/jpg');
Run Code Online (Sandbox Code Playgroud)
完整代码
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:camera_camera/camera_camera.dart';
import 'dart:typed_data';
import 'package:esys_flutter_share/esys_flutter_share.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
File val;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Rully")),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.camera_alt),
onPressed: () async {
val = await showDialog(
context: context,
builder: (context) => Camera(
mode: CameraMode.fullscreen,
orientationEnablePhoto: CameraOrientation.landscape,
/*
imageMask: CameraFocus.square(
color: Colors.black.withOpacity(0.5),
),
*/
));
print("path ${val}");
List<int> bytes = await val.readAsBytes();
Uint8List ubytes = Uint8List.fromList(bytes);
await Share.file('ESYS AMLOG', 'amlog.jpg', ubytes, 'image/jpg');
setState(() {});
}),
body: Center(
child: Container(
height: MediaQuery.of(context).size.height * 0.7,
width: MediaQuery.of(context).size.width * 0.8,
child: val != null
? Image.file(
val,
fit: BoxFit.contain,
)
: Text("Tire a foto"))));
}
}
Run Code Online (Sandbox Code Playgroud)
演示屏幕
在camera_camera示例中,拍照按钮将以横向方式显示 mdoe
文件路径显示在底部
对于相机插件官方示例,我只更改以下
代码片段
void onTakePictureButtonPressed() {
takePicture().then((String filePath) async{
if (mounted) {
setState(() {
imagePath = filePath;
videoController?.dispose();
videoController = null;
});
if (filePath != null) {
showInSnackBar('Picture saved to $filePath');
File val = File(filePath);
List<int> bytes = await val.readAsBytes();
Uint8List ubytes = Uint8List.fromList(bytes);
await Share.file('ESYS AMLOG', 'amlog.jpg', ubytes, 'image/jpg');
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4392 次 |
最近记录: |