fix: get correct downloads directory

This commit is contained in:
Tien Do Nam
2026-08-03 19:18:15 +02:00
parent 1538eccf61
commit 49b23c9b6d
3 changed files with 23 additions and 2 deletions
@@ -7,6 +7,7 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.database.Cursor import android.database.Cursor
import android.net.Uri import android.net.Uri
import android.os.Environment
import android.provider.DocumentsContract import android.provider.DocumentsContract
import android.provider.Settings import android.provider.Settings
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
@@ -79,11 +80,21 @@ class MainActivity : FlutterActivity() {
result.success(isAnimationsEnabled()) result.success(isAnimationsEnabled())
} }
"getDownloadsDirectory" -> {
result.success(getDownloadsDirectory())
}
else -> result.notImplemented() else -> result.notImplemented()
} }
} }
} }
/// Absolute path of the shared "Download" directory (usually /storage/emulated/0/Download).
@Suppress("DEPRECATION")
private fun getDownloadsDirectory(): String {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).absolutePath
}
private fun isAnimationsEnabled() : Boolean { private fun isAnimationsEnabled() : Boolean {
return Settings.Global.getFloat(this.getContentResolver(), return Settings.Global.getFloat(this.getContentResolver(),
Settings.Global.ANIMATOR_DURATION_SCALE, 1.0f) != 0.0f; Settings.Global.ANIMATOR_DURATION_SCALE, 1.0f) != 0.0f;
@@ -39,6 +39,16 @@ Future<List<FileInfo>?> pickFilesAndroid() async {
return result.map((e) => FileInfoMapper.fromJson((e as Map).cast<String, dynamic>())).toList(); return result.map((e) => FileInfoMapper.fromJson((e as Map).cast<String, dynamic>())).toList();
} }
/// Returns the global "Download" directory, e.g. /storage/emulated/0/Download.
Future<String?> getDownloadsDirectoryAndroid() async {
try {
return await _methodChannel.invokeMethod<String>('getDownloadsDirectory');
} catch (e) {
_logger.warning('Could not get downloads directory', e);
return null;
}
}
Future<bool> getSystemAnimationsStatusAndroid() async { Future<bool> getSystemAnimationsStatusAndroid() async {
return await _methodChannel.invokeMethod('isAnimationsEnabled') ?? true; return await _methodChannel.invokeMethod('isAnimationsEnabled') ?? true;
} }
+2 -2
View File
@@ -1,13 +1,13 @@
import 'dart:io' show Directory, Platform; import 'dart:io' show Directory, Platform;
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:localsend_app/util/native/channel/android_channel.dart';
import 'package:path_provider/path_provider.dart' as path; import 'package:path_provider/path_provider.dart' as path;
Future<String> getDefaultDestinationDirectory() async { Future<String> getDefaultDestinationDirectory() async {
switch (defaultTargetPlatform) { switch (defaultTargetPlatform) {
case TargetPlatform.android: case TargetPlatform.android:
final dir = await path.getDownloadsDirectory(); return await getDownloadsDirectoryAndroid() ?? '/storage/emulated/0/Download';
return dir?.path ?? '/storage/emulated/0/Download';
case TargetPlatform.iOS: case TargetPlatform.iOS:
return (await path.getApplicationDocumentsDirectory()).path; return (await path.getApplicationDocumentsDirectory()).path;
case TargetPlatform.linux: case TargetPlatform.linux: