Don't crash with file chooser errors
This commit is contained in:
@@ -101,64 +101,68 @@ public class AndroidLauncher extends AndroidApplication{
|
|||||||
}
|
}
|
||||||
|
|
||||||
void showFileChooser(boolean open, String title, Cons<Fi> cons, String... extensions){
|
void showFileChooser(boolean open, String title, Cons<Fi> cons, String... extensions){
|
||||||
String extension = extensions[0];
|
try{
|
||||||
|
String extension = extensions[0];
|
||||||
|
|
||||||
if(VERSION.SDK_INT >= VERSION_CODES.Q){
|
if(VERSION.SDK_INT >= VERSION_CODES.Q){
|
||||||
Intent intent = new Intent(open ? Intent.ACTION_OPEN_DOCUMENT : Intent.ACTION_CREATE_DOCUMENT);
|
Intent intent = new Intent(open ? Intent.ACTION_OPEN_DOCUMENT : Intent.ACTION_CREATE_DOCUMENT);
|
||||||
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
||||||
intent.setType(extension.equals("zip") && !open && extensions.length == 1 ? "application/zip" : "*/*");
|
intent.setType(extension.equals("zip") && !open && extensions.length == 1 ? "application/zip" : "*/*");
|
||||||
|
|
||||||
addResultListener(i -> startActivityForResult(intent, i), (code, in) -> {
|
addResultListener(i -> startActivityForResult(intent, i), (code, in) -> {
|
||||||
if(code == Activity.RESULT_OK && in != null && in.getData() != null){
|
if(code == Activity.RESULT_OK && in != null && in.getData() != null){
|
||||||
Uri uri = in.getData();
|
Uri uri = in.getData();
|
||||||
|
|
||||||
if(uri.getPath().contains("(invalid)")) return;
|
if(uri.getPath().contains("(invalid)")) return;
|
||||||
|
|
||||||
Core.app.post(() -> Core.app.post(() -> cons.get(new Fi(uri.getPath()){
|
Core.app.post(() -> Core.app.post(() -> cons.get(new Fi(uri.getPath()){
|
||||||
@Override
|
@Override
|
||||||
public InputStream read(){
|
public InputStream read(){
|
||||||
try{
|
try{
|
||||||
return getContentResolver().openInputStream(uri);
|
return getContentResolver().openInputStream(uri);
|
||||||
}catch(IOException e){
|
}catch(IOException e){
|
||||||
throw new ArcRuntimeException(e);
|
throw new ArcRuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public OutputStream write(boolean append){
|
public OutputStream write(boolean append){
|
||||||
try{
|
try{
|
||||||
return getContentResolver().openOutputStream(uri);
|
return getContentResolver().openOutputStream(uri);
|
||||||
}catch(IOException e){
|
}catch(IOException e){
|
||||||
throw new ArcRuntimeException(e);
|
throw new ArcRuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
})));
|
||||||
})));
|
}
|
||||||
}
|
});
|
||||||
});
|
}else if(VERSION.SDK_INT >= VERSION_CODES.M && !(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
|
||||||
}else if(VERSION.SDK_INT >= VERSION_CODES.M && !(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
|
|
||||||
checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)){
|
checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)){
|
||||||
chooser = new FileChooser(title, file -> Structs.contains(extensions, file.extension().toLowerCase()), open, file -> {
|
chooser = new FileChooser(title, file -> Structs.contains(extensions, file.extension().toLowerCase()), open, file -> {
|
||||||
if(!open){
|
if(!open){
|
||||||
cons.get(file.parent().child(file.nameWithoutExtension() + "." + extension));
|
cons.get(file.parent().child(file.nameWithoutExtension() + "." + extension));
|
||||||
}else{
|
}else{
|
||||||
cons.get(file);
|
cons.get(file);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
ArrayList<String> perms = new ArrayList<>();
|
ArrayList<String> perms = new ArrayList<>();
|
||||||
if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
|
if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
|
||||||
perms.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
perms.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
||||||
}
|
}
|
||||||
if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
|
if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
|
||||||
perms.add(Manifest.permission.READ_EXTERNAL_STORAGE);
|
perms.add(Manifest.permission.READ_EXTERNAL_STORAGE);
|
||||||
}
|
}
|
||||||
requestPermissions(perms.toArray(new String[0]), PERMISSION_REQUEST_CODE);
|
requestPermissions(perms.toArray(new String[0]), PERMISSION_REQUEST_CODE);
|
||||||
}else{
|
|
||||||
if(open){
|
|
||||||
new FileChooser(title, file -> Structs.contains(extensions, file.extension().toLowerCase()), true, cons).show();
|
|
||||||
}else{
|
}else{
|
||||||
super.showFileChooser(open, "@open", extension, cons);
|
if(open){
|
||||||
|
new FileChooser(title, file -> Structs.contains(extensions, file.extension().toLowerCase()), true, cons).show();
|
||||||
|
}else{
|
||||||
|
super.showFileChooser(open, "@open", extension, cons);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}catch(Throwable error){
|
||||||
|
Core.app.post(() -> Vars.ui.showException(error));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user