Skip to content

An open-source cross-platform alternative to AirDrop

fix: get correct downloads directory

Tien Do Nam49b23c9b6d93
diff --git a/app/android/app/src/main/kotlin/org/localsend/localsend_app/MainActivity.kt b/app/android/app/src/main/kotlin/org/localsend/localsend_app/MainActivity.kt
index 8ddc23b7..b17633aa 100644
--- a/app/android/app/src/main/kotlin/org/localsend/localsend_app/MainActivity.kt
+++ b/app/android/app/src/main/kotlin/org/localsend/localsend_app/MainActivity.kt
@@ -7,6 +7,7 @@ import android.content.Context
import android.content.Intent
import android.database.Cursor
import android.net.Uri
+import android.os.Environment
import android.provider.DocumentsContract
import android.provider.Settings
import io.flutter.embedding.android.FlutterActivity
@@ -79,11 +80,21 @@ class MainActivity : FlutterActivity() {
result.success(isAnimationsEnabled())
}
+ "getDownloadsDirectory" -> {
+ result.success(getDownloadsDirectory())
+ }
+
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 {
return Settings.Global.getFloat(this.getContentResolver(),
Settings.Global.ANIMATOR_DURATION_SCALE, 1.0f) != 0.0f;
diff --git a/app/lib/util/native/channel/android_channel.dart b/app/lib/util/native/channel/android_channel.dart
index d59c930e..6eb64182 100644
--- a/app/lib/util/native/channel/android_channel.dart
+++ b/app/lib/util/native/channel/android_channel.dart
@@ -39,6 +39,16 @@ Future<List<FileInfo>?> pickFilesAndroid() async {
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 {
return await _methodChannel.invokeMethod('isAnimationsEnabled') ?? true;
}
diff --git a/app/lib/util/native/directories.dart b/app/lib/util/native/directories.dart
index 56feb046..fed0112b 100644
--- a/app/lib/util/native/directories.dart
+++ b/app/lib/util/native/directories.dart
@@ -1,13 +1,13 @@
import 'dart:io' show Directory, Platform;
import 'package:flutter/foundation.dart';
+import 'package:localsend_app/util/native/channel/android_channel.dart';
import 'package:path_provider/path_provider.dart' as path;
Future<String> getDefaultDestinationDirectory() async {
switch (defaultTargetPlatform) {
case TargetPlatform.android:
- final dir = await path.getDownloadsDirectory();
- return dir?.path ?? '/storage/emulated/0/Download';
+ return await getDownloadsDirectoryAndroid() ?? '/storage/emulated/0/Download';
case TargetPlatform.iOS:
return (await path.getApplicationDocumentsDirectory()).path;
case TargetPlatform.linux: