This commit is contained in:
bluesea 2024-05-14 12:03:09 +08:00
parent a3b23b4df5
commit 6d393401c0
5 changed files with 71 additions and 3 deletions

View File

@ -1,9 +1,11 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:wallpaper/utils/downloadmanager.dart';
// import 'TestApp.dart';
import 'WallPaperApp.dart';
void main() {
Future<void> main() async {
await DownloadManager.downloadImgWithUrl("https://resource-sg-public.lux-ad.com/wallpaper/4f1e7f66f26003b0ea83f4934302ce84.jpg");
runApp(const WallPaperApp());
}

View File

@ -0,0 +1,19 @@
import 'dart:io';
import 'package:flutter/foundation.dart';
class DownloadManager {
static Future<void> downloadImgWithUrl(String urlStr) async {
var httpClient = HttpClient();
try {
var reqiest = await httpClient.getUrl(Uri.parse(urlStr));
var response = await reqiest.close();
// var bytes = await consolidateHttpClientResponseBytes(response);
var bytes = await consolidateHttpClientResponseBytes(response);
print("the bytes:${bytes}");
}
catch(error) {
print("the error:${error}");
}
}
}

View File

@ -0,0 +1,40 @@
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';
File imageFile=File(filePath);
if(imageFile.existsSync()){
print("图片已存在");
return "";
}
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;
}
}

View File

@ -154,7 +154,7 @@ packages:
source: hosted
version: "3.1.1"
crypto:
dependency: transitive
dependency: "direct main"
description:
name: crypto
sha256: ff625774173754681d66daaf4a448684fb04b78f902da9cb3d308c19cc5e8bab
@ -497,7 +497,7 @@ packages:
source: hosted
version: "1.9.0"
path_provider:
dependency: transitive
dependency: "direct main"
description:
name: path_provider
sha256: c9e7d3a4cd1410877472158bee69963a4579f78b68c65a2b7d40d1a7a88bb161

View File

@ -49,6 +49,12 @@ dependencies:
hive: ^2.2.3
hive_flutter: ^1.1.0
#获取本地文件的缓存路径
path_provider: ^2.1.3
#随机生成文件名
crypto: ^3.0.3
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.6
@ -57,6 +63,7 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
#数据库
hive_generator: ^ 2.0.1
build_runner: ^ 2.4.9
# The "flutter_lints" package below contains a set of recommended lints to