r_album 0.1.5
r_album: ^0.1.5 copied to clipboard
A Flutter Plugin about image or video file save in album ,support Android and IOS.
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:r_album/r_album.dart';
import 'package:r_album_example/video_widget.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
File? _file;
bool? _isSaving;
bool? isClickVideo;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: _isSaving == true
? Center(child: CircularProgressIndicator())
: ListView(
children: <Widget>[
_buildPickedImage(),
_buildImagePickerButton(),
_buildVideoPickerButton(),
_buildSaveToAlbumButton(),
_buildCreateAlbumButton(),
],
),
),
);
}
Widget _buildPickedImage() {
return Container(
margin: EdgeInsets.all(16),
height: 300.0,
child: isClickVideo != null
? isClickVideo!
? VideoWidget(
file: _file!,
)
: Image.file(
_file!,
fit: BoxFit.fill,
)
: Center(
child: Container(
child: Text("No selected image or video"),
),
),
decoration: isClickVideo != null
? null
: BoxDecoration(border: Border.all(width: 1)),
);
}
Widget _buildImagePickerButton() {
return Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
IconButton(
tooltip: 'select image from camera',
icon: Icon(Icons.camera_alt),
onPressed: () async {
var image =
await ImagePicker().pickImage(source: ImageSource.camera);
if (image != null) {
isClickVideo = false;
setState(() {
_file = File(image.path);
});
}
},
),
IconButton(
tooltip: 'select image from gallery',
icon: Icon(Icons.folder),
onPressed: () async {
var image =
await ImagePicker().pickImage(source: ImageSource.gallery);
if (image != null) {
isClickVideo = false;
setState(() {
_file = File(image.path);
});
}
},
),
],
),
);
}
Widget _buildVideoPickerButton() {
return Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
IconButton(
tooltip: 'select video from camera',
icon: Icon(Icons.videocam),
onPressed: () async {
var video =
await ImagePicker().pickVideo(source: ImageSource.camera);
if (video != null) {
isClickVideo = true;
setState(() {
_file = File(video.path);
});
}
},
),
IconButton(
tooltip: 'select video from gallery',
icon: Icon(Icons.folder),
onPressed: () async {
var video =
await ImagePicker().pickVideo(source: ImageSource.gallery);
if (video != null) {
isClickVideo = true;
setState(() {
_file = File(video.path);
});
}
},
),
],
),
);
}
Widget _buildSaveToAlbumButton() {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: ElevatedButton(
child: Text("Save to album"),
onPressed: () async {
setState(() {
_isSaving = true;
});
if (await canReadStorage()) {
bool? isSuccess = await RAlbum.saveAlbum(
"MyTestAlbum",
[_file!.path, _file!.path],
);
print('保存图片到相册是否成功:$isSuccess');
setState(() {
_isSaving = false;
});
}
},
),
);
}
Future<bool> canReadStorage() async {
if (Platform.isIOS) return true;
var status = await Permission.storage.status;
if (status != PermissionStatus.granted) {
var future = await Permission.storage.request();
if (future != PermissionStatus.granted) {
return false;
}
} else {
return true;
}
return true;
}
Widget _buildCreateAlbumButton() {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: ElevatedButton(
child: Text("Create a album named MyTestAlbum"),
onPressed: () async {
if (await canReadStorage()) {
setState(() {
_isSaving = true;
});
bool? isSuccess = await RAlbum.createAlbum("MyTestAlbum");
print('创建相册是否成功:$isSuccess');
setState(() {
_isSaving = false;
});
}
},
),
);
}
}