40 lines
1.0 KiB
Dart
40 lines
1.0 KiB
Dart
import 'dart:io';
|
|
import 'dart:typed_data';
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'dart:math';
|
|
import 'dart:convert';
|
|
|
|
import 'package:crypto/crypto.dart';
|
|
|
|
class LocalImgManager {
|
|
static Future<String> getImgLocalDir() async {
|
|
Directory dir = await getTemporaryDirectory();
|
|
return dir.path;
|
|
}
|
|
|
|
static Future<String> saveImgDataBytes(Uint8List data) async {
|
|
String fileName = "img_${LocalImgManager.sha1RandomString()}.jpg";
|
|
var localDir = await getImgLocalDir();
|
|
var filePath = '$localDir/$fileName';
|
|
// print("the filepath:$filePath ");
|
|
File imageFile=File(filePath);
|
|
if(imageFile.existsSync()){
|
|
print("图片已存在");
|
|
return "";
|
|
}
|
|
await imageFile.writeAsBytes(data);
|
|
return fileName;
|
|
}
|
|
|
|
|
|
static String sha1RandomString() {
|
|
final randomNumber = Random().nextDouble();
|
|
final randomBytes = utf8.encode(randomNumber.toString());
|
|
final randomString = sha1.convert(randomBytes).toString();
|
|
return randomString;
|
|
}
|
|
|
|
} |