refactor(android): update android extension configuration

This commit is contained in:
2026-11-01 16:26:56 +03:00
parent 1d64087e40
commit eeee6e1f18
5 changed files with 23 additions and 16 deletions
+2
View File
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ru.m.android" />
@@ -0,0 +1,55 @@
package ru.m.android;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import org.haxe.extension.Extension;
import org.haxe.lime.HaxeObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class FileUtil extends Extension {
final private static int REQUEST_CODE = 123;
private static HaxeObject callback;
public static void browse(HaxeObject callback) {
FileUtil.callback = callback;
Intent intent = new Intent()
.setType("image/*")
.setAction(Intent.ACTION_GET_CONTENT);
Extension.mainActivity.startActivityForResult(Intent.createChooser(intent, "Select a file"), REQUEST_CODE);
}
@Override
public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Uri uri = data.getData(); //The uri with the location of the file
try {
InputStream is = Extension.mainActivity.getContentResolver().openInputStream(uri);
byte[] result = getBytes(is);
callback.call("onSuccess", new Object[]{result});
} catch (IOException exception) {
Log.e("FileUtil", "", exception);
callback.call("onFail", new Object[]{exception});
}
return true;
} else {
return super.onActivityResult(requestCode, resultCode, data);
}
}
public static byte[] getBytes(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
return byteBuffer.toByteArray();
}
}