From 127b32a69a6e68e887efefe94a874a5dea4b25cf Mon Sep 17 00:00:00 2001 From: kairusds <32636532+kairusds@users.noreply.github.com> Date: Thu, 17 Oct 2024 09:18:42 +0800 Subject: [PATCH] --- .../libvlcandroidtest/MainActivity.java | 108 +++++++++--------- 1 file changed, 56 insertions(+), 52 deletions(-) diff --git a/app/src/main/java/github/kairusds/libvlcandroidtest/MainActivity.java b/app/src/main/java/github/kairusds/libvlcandroidtest/MainActivity.java index d32fb48..6d78548 100644 --- a/app/src/main/java/github/kairusds/libvlcandroidtest/MainActivity.java +++ b/app/src/main/java/github/kairusds/libvlcandroidtest/MainActivity.java @@ -191,67 +191,71 @@ private void setVLCMedia(String path){ } private void showFilePickerDialog(File dir){ - executor.execute(() -> { - File[] files = dir.listFiles(); - ArrayList fileList = new ArrayList<>(); - HashMap paths = new HashMap<>(); - fileList.add(".."); - - if(files != null){ - Tika tika = new Tika(); - for(File file : files){ - if(file.isDirectory()){ - fileList.add(file.getName()); - paths.put(file.getName(), file.getAbsolutePath()); - }else{ - String mimetype = tika.detect(file.getName()); - if(mimetype.startsWith("video/")){ + try{ + executor.execute(() -> { + File[] files = dir.listFiles(); + ArrayList fileList = new ArrayList<>(); + HashMap paths = new HashMap<>(); + fileList.add(".."); + + if(files != null){ + Tika tika = new Tika(); + for(File file : files){ + if(file.isDirectory()){ fileList.add(file.getName()); paths.put(file.getName(), file.getAbsolutePath()); + }else{ + String mimetype = tika.detect(file.getName()); + if(mimetype.startsWith("video/")){ + fileList.add(file.getName()); + paths.put(file.getName(), file.getAbsolutePath()); + } } } } - } - - handler.post(() -> { - if(files == null){ - toast("Unable to access directory"); - executor.shutdown(); - executor = null; - return; - } - - ArrayAdapter adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.select_dialog_item, fileList); - AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); - builder.setTitle("Choose a file or directory"); - builder.setAdapter(adapter, (dialog, which) -> { - if(which == 0){ - File parentDir = dir.getParentFile(); - if(parentDir != null){ - dialog.dismiss(); - showFilePickerDialog(parentDir); - }else{ - toast("No parent directory"); - } - }else{ - String filename = fileList.get(which); - String filepath = paths.get(filename); - File selectedFile = new File(filepath); - if(selectedFile.isDirectory()){ - dialog.dismiss(); - showFilePickerDialog(selectedFile); + + handler.post(() -> { + if(files == null){ + toast("Unable to access directory"); + executor.shutdown(); + executor = null; + return; + } + + ArrayAdapter adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.select_dialog_item, fileList); + AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); + builder.setTitle("Choose a file or directory"); + builder.setAdapter(adapter, (dialog, which) -> { + if(which == 0){ + File parentDir = dir.getParentFile(); + if(parentDir != null){ + dialog.dismiss(); + showFilePickerDialog(parentDir); + }else{ + toast("No parent directory"); + } }else{ - setVLCMedia(filepath); - dialog.dismiss(); - executor.shutdown(); - executor = null; + String filename = fileList.get(which); + String filepath = paths.get(filename); + File selectedFile = new File(filepath); + if(selectedFile.isDirectory()){ + dialog.dismiss(); + showFilePickerDialog(selectedFile); + }else{ + setVLCMedia(filepath); + dialog.dismiss(); + executor.shutdown(); + executor = null; + } } - } + }); + builder.setNegativeButton("Cancel", null); + builder.show(); }); - builder.setNegativeButton("Cancel", null); - builder.show(); }); - }); + }catch(Exception e){ + toast(e.getMessage()); + } } @Override