Skip to content

An open-source cross-platform alternative to AirDrop

fix: crash when picking files or folders on Android TV

Tien Do Nam6ef047b4454e
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 09a0131e..0fd1c11e 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
@@ -45,6 +45,7 @@ class MainActivity : FlutterActivity() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
+ putExtra("multi-pick", true)
type = "*/*"
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
}
@@ -53,10 +54,16 @@ class MainActivity : FlutterActivity() {
@SuppressLint("WrongConstant")
@Override
- override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
+ override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
- if (resultCode != Activity.RESULT_OK) {
- pendingResult?.error("Error", "Failed to access directory", null)
+ if (resultCode == Activity.RESULT_CANCELED) {
+ pendingResult?.error("CANCELED", "Canceled", null)
+ pendingResult = null
+ return
+ }
+
+ if (resultCode != Activity.RESULT_OK || data == null) {
+ pendingResult?.error("Error $resultCode", "Failed to access directory or file", null)
pendingResult = null
return
}
diff --git a/app/assets/CHANGELOG.md b/app/assets/CHANGELOG.md
index 27a20bcb..69678732 100644
--- a/app/assets/CHANGELOG.md
+++ b/app/assets/CHANGELOG.md
@@ -3,6 +3,7 @@
- feat: support Internet Explorer 8 (IE8) in web share (@Tienisto)
- feat: save auto accept state when switching encryption mode in web share (@Tienisto)
- fix: count PIN tries correctly in web share (@Tienisto)
+- fix(android): crash when picking files or folders on Android TV (@Tienisto)
- fix(windows): crash when sum of file sizes greater than 2 GB (@Tienisto)
- fix(windows): bundle required DLLs files to avoid crash on app start (@Tienisto)
diff --git a/app/lib/util/native/file_picker.dart b/app/lib/util/native/file_picker.dart
index efa11014..4588eabf 100644
--- a/app/lib/util/native/file_picker.dart
+++ b/app/lib/util/native/file_picker.dart
@@ -163,6 +163,12 @@ Future<void> _pickFiles(BuildContext context, Ref ref) async {
));
}
} catch (e) {
+ if (e is PlatformException && e.code == 'CANCELED') {
+ // User canceled the file picker
+ _logger.info('User canceled file picker');
+ return;
+ }
+
// ignore: use_build_context_synchronously
await showDialog(context: context, builder: (_) => const NoPermissionDialog());
_logger.warning('Failed to pick files', e);
@@ -206,6 +212,12 @@ Future<void> _pickFolder(BuildContext context, Ref ref) async {
}
}
} catch (e) {
+ if (e is PlatformException && e.code == 'CANCELED') {
+ // User canceled the file picker
+ _logger.info('User canceled file picker');
+ return;
+ }
+
_logger.warning('Failed to pick directory', e);
// ignore: use_build_context_synchronously
await showDialog(context: context, builder: (_) => const NoPermissionDialog());