This repository was archived by the owner on Apr 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
67 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters