我正在尝试与其他用户共享从颤振相机拍摄的图像。
问题在于,当我尝试共享图像时,出现错误消息:该图像不存在,使我认为存储该图像的临时目录在下一个屏幕出现时不再存在。我的问题是,我可以将其保存到手机上的其他位置以便它仍然存在吗?我正在使用android。这是我得到的错误:
Unhandled Exception: FileSystemException: Cannot create file, path = '/data/user/0/com.example.pictureapp/cache//data/user/0/com.example.pictureapp/cache/2020-03-20 20:10:24.403289.png' (OS Error: No such file or directory, errno = 2)
我有以下代码:
onPressed: () async {
// Take the Picture in a try / catch block. If anything goes wrong,
// catch the error.
try {
// Ensure that the camera is initialized.
await _initializeControllerFuture;
/////////////////////////////////////////////////////////
//below is the code I believe needs to be changed
////////////////////////////////////////////////////////
final path = join(
// Store the picture in the temp directory.
// Find the temp directory using the `path_provider` plugin.
(await getTemporaryDirectory()).path,
'${DateTime.now()}.png',
);
// Attempt to take a picture and log where it's been saved.
await _controller.takePicture(path);
// If the picture was taken, display it on a new screen.
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DisplayPictureScreen(imagePath: path),
),
);
} catch (e) {
// If an error occurs, log the error to the console.
print(e);
}
},
),
);
}
}
// A widget that displays the picture taken by the user.
class DisplayPictureScreen extends StatelessWidget {
final String imagePath;
Future<ByteData> getBytesFromFile() async {
Uint8List bytes = File(imagePath).readAsBytesSync() as Uint8List;
return ByteData.view(bytes.buffer);
}
const DisplayPictureScreen({Key key, this.imagePath}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Display the Picture')),
// The image is stored as a file on the device. Use the `Image.file`
// constructor with the given path to display the image.
body: Image.file(File(imagePath)),
floatingActionButton: FloatingActionButton(
onPressed: () {
/*
print("in print");
Share.share(imagePath);
*/
getBytesFromFile().then((bytes) {
Share.file('Share via:', imagePath,
bytes.buffer.asUint8List(), 'image/png');
});
}
)
);
}
}
答案 0 :(得分:1)
这是从相机拍摄照片的代码。使用此onPressed方法。
final Directory extDir =
await getTemporaryDirectory(); //or you can use getApplicationDocumentsDirectory();
final String dirPath = '${extDir.path}/appName/Camera/Images';
//I think you are missing below line of code.
await new Directory(dirPath).create(recursive: true);
final String filePath = '$dirPath/${timestamp()}.jpg';
if (_controller.value.isTakingPicture) {
// A capture is already pending, do nothing.
//return null;
}
try {
await _controller.takePicture(filePath);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DisplayPictureScreen(imagePath:filePath),
),
);
} on CameraException catch (e) {
showException(e);
}
时间戳方法
String timestamp() => new DateTime.now().millisecondsSinceEpoch.toString();
答案 1 :(得分:0)
/data/user/0/com.example.pictureapp/cache//data/user/0/com.example.pictureapp/cache/2020-03-20 20:10:24.403289.png
看起来好像您两次附加了temp dir路径。从已发布的代码中尚不清楚此重复发生的位置,但逐步进行操作应该很容易。