Skip to content
This repository was archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
🐛 修复了存储快照对Android API 30以下的系统出错的BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
dzxrly committed Feb 11, 2024
1 parent bf75fc2 commit 9e52698
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 16 deletions.
1 change: 0 additions & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:name="${applicationName}"
android:enableOnBackInvokedCallback="true"
Expand Down
2 changes: 1 addition & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@
"@exportPlayerStateBtnTitle": {
"description": "Title of the export player state button"
},
"exportPlayerState": "{exportPlayerState, select, success{Exported successfully !} error{Failed to export the player state} denied{Permission denied, please allow the storage access permission} other{exportPlayerState}}",
"exportPlayerState": "{exportPlayerState, select, success{Exported successfully !} error{Failed to export the player stat snapshot} denied{Permission denied, please allow the storage access permission} other{exportPlayerState}}",
"@exportPlayerState": {
"description": "Tip for the export player state",
"placeholders": {
Expand Down
53 changes: 40 additions & 13 deletions lib/utils/file_exporter/android_file_exporter.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,53 @@
import 'dart:developer';
import 'dart:io';

import 'package:battlefield_2042_state/utils/tools.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:path_provider/path_provider.dart';
import 'package:saf/saf.dart';

FileExporter fileExporter() => AndroidFileExporter();

class AndroidFileExporter implements FileExporter {
@override
Future exportFile(String fileName, String content) async {
final safInstance = Saf('/storage/emulated/0/Documents');
bool? isGranted = await safInstance.getDirectoryPermission(isDynamic: true);
if (isGranted != null && isGranted) {
// Perform some file operations
final file = File('/storage/emulated/0/Documents/$fileName');
await file
.writeAsString(content)
.then((value) => {Future.value('success')})
.catchError((error) {
Future.error('error');
});
} else {
return Future.error('denied');
// in API 30 and above, dir is Documents, in API 29 and below, dir is Downloads
try {
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
var dir = '/storage/emulated/0/Documents';
if (androidInfo.version.sdkInt <= 30) {
final Directory? downloadsDir = await getDownloadsDirectory();
if (downloadsDir != null) {
dir = downloadsDir.path.toString();
} else {
// create a Downloads directory in app directory
final Directory appDocDir = await getApplicationDocumentsDirectory();
dir = '${appDocDir.path}/Downloads';
final Directory newDir = Directory(dir);
if (!newDir.existsSync()) {
newDir.createSync();
}
}
}
log(dir);
final safInstance = Saf(dir);
bool? isGranted =
await safInstance.getDirectoryPermission(isDynamic: true);
if (isGranted != null && isGranted) {
// Perform some file operations
final file = File('$dir/$fileName');
await file
.writeAsString(content)
.then((value) => {Future.value('success')})
.catchError((error) {
throw 'error';
});
} else {
throw 'denied';
}
} catch (e) {
return Future.error('error');
}
}
}
24 changes: 24 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.3.4"
device_info_plus:
dependency: "direct main"
description:
name: device_info_plus
sha256: "77f757b789ff68e4eaf9c56d1752309bd9f7ad557cb105b938a7f8eb89e59110"
url: "https://pub.dev"
source: hosted
version: "9.1.2"
device_info_plus_platform_interface:
dependency: transitive
description:
name: device_info_plus_platform_interface
sha256: d3b01d5868b50ae571cd1dc6e502fc94d956b665756180f7b16ead09e836fd64
url: "https://pub.dev"
source: hosted
version: "7.0.0"
fake_async:
dependency: transitive
description:
Expand Down Expand Up @@ -858,6 +874,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "5.2.0"
win32_registry:
dependency: transitive
description:
name: win32_registry
sha256: "41fd8a189940d8696b1b810efb9abcf60827b6cbfab90b0c43e8439e3a39d85a"
url: "https://pub.dev"
source: hosted
version: "1.1.2"
xdg_directories:
dependency: transitive
description:
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 2.2.9+67
version: 2.2.9+68

environment:
sdk: '>=3.1.3 <4.0.0'
Expand Down Expand Up @@ -51,6 +51,7 @@ dependencies:
path_provider: ^2.1.2
url_launcher: ^6.2.4
saf: ^1.0.3+4
device_info_plus: ^9.1.2

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 9e52698

Please sign in to comment.