64 lines
1.8 KiB
Dart
64 lines
1.8 KiB
Dart
import 'dart:typed_data';
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
/// Hasil penyimpanan file ke folder Download.
|
|
class SavedDownloadFile {
|
|
final String uri;
|
|
final String displayPath;
|
|
|
|
const SavedDownloadFile({required this.uri, required this.displayPath});
|
|
}
|
|
|
|
/// Jembatan ke MediaStore Android untuk menyimpan file ke folder Download.
|
|
class MediaStoreService {
|
|
static const MethodChannel _channel = MethodChannel('cashumit/media_store');
|
|
|
|
/// Simpan [bytes] ke folder Download. Return null jika gagal.
|
|
static Future<SavedDownloadFile?> saveToDownloads({
|
|
required Uint8List bytes,
|
|
required String fileName,
|
|
String mimeType = 'image/png',
|
|
}) async {
|
|
try {
|
|
final result = await _channel.invokeMapMethod<String, dynamic>(
|
|
'saveToDownloads',
|
|
{
|
|
'bytes': bytes,
|
|
'fileName': fileName,
|
|
'mimeType': mimeType,
|
|
},
|
|
);
|
|
if (result == null) return null;
|
|
final uri = result['uri']?.toString() ?? '';
|
|
final displayPath = result['displayPath']?.toString() ?? fileName;
|
|
if (uri.isEmpty) return null;
|
|
return SavedDownloadFile(uri: uri, displayPath: displayPath);
|
|
} on PlatformException {
|
|
return null;
|
|
} on MissingPluginException {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// Buka file hasil simpan (content URI atau path file).
|
|
static Future<bool> openFile({
|
|
String? uri,
|
|
String? path,
|
|
String mimeType = 'image/png',
|
|
}) async {
|
|
try {
|
|
final result = await _channel.invokeMethod<bool>('openFile', {
|
|
if (uri != null) 'uri': uri,
|
|
if (path != null) 'path': path,
|
|
'mimeType': mimeType,
|
|
});
|
|
return result ?? false;
|
|
} on PlatformException {
|
|
return false;
|
|
} on MissingPluginException {
|
|
return false;
|
|
}
|
|
}
|
|
}
|