feat:调整上传逻辑
This commit is contained in:
@@ -129,6 +129,71 @@ class _LookImagesWidgetState extends State<LookImagesWidget> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// 可选:根据字节头自动识别文件后缀(如图片、视频)
|
||||||
|
String getExtension(Uint8List uint8List) {
|
||||||
|
if (uint8List.length < 4) return "bin"; // 无法识别时返回二进制后缀
|
||||||
|
|
||||||
|
// PNG 头:89 50 4E 47
|
||||||
|
if (uint8List[0] == 0x89 &&
|
||||||
|
uint8List[1] == 0x50 &&
|
||||||
|
uint8List[2] == 0x4E &&
|
||||||
|
uint8List[3] == 0x47) {
|
||||||
|
return "png";
|
||||||
|
}
|
||||||
|
// JPG 头:FF D8 FF
|
||||||
|
else if (uint8List[0] == 0xFF && uint8List[1] == 0xD8 && uint8List[2] == 0xFF) {
|
||||||
|
return "jpg";
|
||||||
|
}
|
||||||
|
// MP4 头:00 00 00 18 66 74 79 70
|
||||||
|
else if (uint8List.length >= 8 &&
|
||||||
|
uint8List[0] == 0x00 &&
|
||||||
|
uint8List[1] == 0x00 &&
|
||||||
|
uint8List[2] == 0x00 &&
|
||||||
|
uint8List[3] == 0x18 &&
|
||||||
|
uint8List[4] == 0x66 &&
|
||||||
|
uint8List[5] == 0x74 &&
|
||||||
|
uint8List[6] == 0x79 &&
|
||||||
|
uint8List[7] == 0x70) {
|
||||||
|
return "mp4";
|
||||||
|
}
|
||||||
|
// 其他格式可自行扩展(如 GIF、PDF 等)
|
||||||
|
else {
|
||||||
|
return "bin";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Uint8List 转临时 File
|
||||||
|
Future<File?> uint8ListToTempFile(Uint8List uint8List, {String fileName = "temp_file"}) async {
|
||||||
|
try {
|
||||||
|
// 1. 获取临时存储目录(跨平台兼容)
|
||||||
|
Directory tempDir = await getTemporaryDirectory();
|
||||||
|
String tempPath = tempDir.path;
|
||||||
|
|
||||||
|
// 2. 拼接文件路径(可自定义后缀,如 .png、.mp4 等)
|
||||||
|
File tempFile = File("$tempPath/$fileName.${getExtension(uint8List)}"); // 自动识别后缀(可选)
|
||||||
|
|
||||||
|
// 3. 将 Uint8List 写入文件
|
||||||
|
await tempFile.writeAsBytes(uint8List);
|
||||||
|
print("临时文件路径:${tempFile.path}");
|
||||||
|
String imageUrl = await UploadOss.upload(
|
||||||
|
tempFile.path,
|
||||||
|
fileType: getExtension(uint8List),
|
||||||
|
oSSAccessKeyId: widget.oSSAccessKeyId ?? '',
|
||||||
|
ossHost: widget.ossHost ?? '',
|
||||||
|
ossDirectory: widget.ossDirectory ?? '',
|
||||||
|
policy: widget.policy ?? '',
|
||||||
|
callback: widget.callback ?? '',
|
||||||
|
signature: widget.signature ?? '',
|
||||||
|
);
|
||||||
|
print("上传后的路径:$imageUrl");
|
||||||
|
return tempFile;
|
||||||
|
} catch (e) {
|
||||||
|
print("转换临时文件失败:$e");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
listData = widget.listData;
|
listData = widget.listData;
|
||||||
@@ -173,69 +238,7 @@ class _LookImagesWidgetState extends State<LookImagesWidget> {
|
|||||||
child: GestureDetector(
|
child: GestureDetector(
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
Uint8List? imageFile = await editImage(url: listData[currentPage]);
|
Uint8List? imageFile = await editImage(url: listData[currentPage]);
|
||||||
|
uint8ListToTempFile(imageFile??Uint8List(0));
|
||||||
/// 可选:根据字节头自动识别文件后缀(如图片、视频)
|
|
||||||
String getExtension(Uint8List uint8List) {
|
|
||||||
if (uint8List.length < 4) return "bin"; // 无法识别时返回二进制后缀
|
|
||||||
|
|
||||||
// PNG 头:89 50 4E 47
|
|
||||||
if (uint8List[0] == 0x89 &&
|
|
||||||
uint8List[1] == 0x50 &&
|
|
||||||
uint8List[2] == 0x4E &&
|
|
||||||
uint8List[3] == 0x47) {
|
|
||||||
return "png";
|
|
||||||
}
|
|
||||||
// JPG 头:FF D8 FF
|
|
||||||
else if (uint8List[0] == 0xFF && uint8List[1] == 0xD8 && uint8List[2] == 0xFF) {
|
|
||||||
return "jpg";
|
|
||||||
}
|
|
||||||
// MP4 头:00 00 00 18 66 74 79 70
|
|
||||||
else if (uint8List.length >= 8 &&
|
|
||||||
uint8List[0] == 0x00 &&
|
|
||||||
uint8List[1] == 0x00 &&
|
|
||||||
uint8List[2] == 0x00 &&
|
|
||||||
uint8List[3] == 0x18 &&
|
|
||||||
uint8List[4] == 0x66 &&
|
|
||||||
uint8List[5] == 0x74 &&
|
|
||||||
uint8List[6] == 0x79 &&
|
|
||||||
uint8List[7] == 0x70) {
|
|
||||||
return "mp4";
|
|
||||||
}
|
|
||||||
// 其他格式可自行扩展(如 GIF、PDF 等)
|
|
||||||
else {
|
|
||||||
return "bin";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Uint8List 转临时 File
|
|
||||||
Future<File?> uint8ListToTempFile(Uint8List uint8List, {String fileName = "temp_file"}) async {
|
|
||||||
try {
|
|
||||||
// 1. 获取临时存储目录(跨平台兼容)
|
|
||||||
Directory tempDir = await getTemporaryDirectory();
|
|
||||||
String tempPath = tempDir.path;
|
|
||||||
|
|
||||||
// 2. 拼接文件路径(可自定义后缀,如 .png、.mp4 等)
|
|
||||||
File tempFile = File("$tempPath/$fileName.${getExtension(uint8List)}"); // 自动识别后缀(可选)
|
|
||||||
|
|
||||||
// 3. 将 Uint8List 写入文件
|
|
||||||
await tempFile.writeAsBytes(uint8List);
|
|
||||||
print("临时文件路径:${tempFile.path}");
|
|
||||||
String string = await UploadOss.upload(
|
|
||||||
tempFile.path,
|
|
||||||
fileType: getExtension(uint8List),
|
|
||||||
oSSAccessKeyId: widget.oSSAccessKeyId ?? '',
|
|
||||||
ossHost: widget.ossHost ?? '',
|
|
||||||
ossDirectory: widget.ossDirectory ?? '',
|
|
||||||
policy: widget.policy ?? '',
|
|
||||||
callback: widget.callback ?? '',
|
|
||||||
signature: widget.signature ?? '',
|
|
||||||
);
|
|
||||||
return tempFile;
|
|
||||||
} catch (e) {
|
|
||||||
print("转换临时文件失败:$e");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
child: Icon(Icons.edit, color: Colors.white))),
|
child: Icon(Icons.edit, color: Colors.white))),
|
||||||
//图片张数指示器
|
//图片张数指示器
|
||||||
|
|||||||
Reference in New Issue
Block a user