flutter 下载 项目资源图片 和截屏
最近开发项目遇到 一个需求 下载一张项目内的图片,开始是截屏,后来要求直接下载图片。 先在yaml 文件中引用
permission_handler: ^9.2.0
permission_handler_platform_interface: ^3.7.0
# 文件保存插件
image_gallery_saver: ^1.7.1
在 清单文件加权限
<!--允许程序设置内置sd卡的读写权限-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
首先申请权限
Map<Permission, PermissionStatus> statuses = await [
Permission.storage,
].request();
final info = statuses[Permission.storage].toString();
如果是 下载资源文件
/// localurl assert 路径
/// fileName 文件名称
static void saveAassetsImage(String localurl,String fileName) async {
var bytes = await rootBundle.load(localurl);
ByteBuffer buf = bytes.buffer;
Uint8List picBytes = buf.asUint8List();
final result = await ImageGallerySaver.saveImage(picBytes,
quality: 100, name: fileName);
if (result['isSuccess']) {
print('保存成功');
} else {
print('保存失败');
}
}
如果是 截屏 需要用 RepaintBoundary +key 的方式
RepaintBoundary(
key: globalKey,// 通过这个key 确定 截图范围
child: Image(
image: const AssetImage('assets/images/111.png'),
width: 200,
height: 150,
),
),
截屏方法如下
RenderRepaintBoundary boundary =
globalKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage();
ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List picBytes = byteData!.buffer.asUint8List();
final result = await ImageGallerySaver.saveImage(picBytes,
quality: quali, name: nameString);
if (result['isSuccess']) {
if (bools) {
print('保存成功');
}
} else {
print('保存失败');
}
我抽取成一个方法类
/// refix_name_page.dart
/// cmzjapp
///
/// Created by 介星星 on 2022/1/21.
/// Copyright ? 2022年 cmzjapp. All rights reserved.
///
/// @Description 图片的保存 和 分享
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:permission_handler/permission_handler.dart';
class ImageSaveUtils {
/// 保存图片需要动态申请权限 requestPermission();
static requestPermission() async {
Map<Permission, PermissionStatus> statuses = await [
Permission.storage,
].request();
final info = statuses[Permission.storage].toString();
print(info);
}
static Future<void> save(globalKey, nameString,
{int quali = 100, bool bools = true}) async {
RenderRepaintBoundary boundary =
globalKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage();
ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List picBytes = byteData!.buffer.asUint8List();
final result = await ImageGallerySaver.saveImage(picBytes,
quality: quali, name: nameString);
if (result['isSuccess']) {
if (bools) {
print('保存成功');
}
} else {
print('保存失败');
}
}
/// localurl assert 路径
/// fileName 文件名称
static void saveAassetsImage(String localurl,String fileName) async {
var bytes = await rootBundle.load(localurl);
ByteBuffer buf = bytes.buffer;
Uint8List picBytes = buf.asUint8List();
final result = await ImageGallerySaver.saveImage(picBytes,
quality: 100, name: fileName);
if (result['isSuccess']) {
print('保存成功');
} else {
print('保存失败');
}
}
}
具体调用 截屏使用
ImageSaveUtils.requestPermission(); 先申请权限
int current = DateTime.now().millisecondsSinceEpoch;
ImageSaveUtils.save(globalKey, "mn_" + current.toString());
下载assert 图片
ImageSaveUtils.requestPermission(); 先申请权限
ImageSaveUtils.saveAassetsImage('assets/images/222.png', 'xiaomao'); 下载
下载地址 https://download.csdn.net/download/bu_wangchuxin/85213604 (刚刚上传 估计几天就可以下载)
|