[add] FileSource
This commit is contained in:
57
dependencies/android/src/ru/m/android/FileUtil.java
vendored
Normal file
57
dependencies/android/src/ru/m/android/FileUtil.java
vendored
Normal file
@@ -0,0 +1,57 @@
|
||||
package ru.m.android;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.res.AssetManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.view.View;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
import org.haxe.extension.Extension;
|
||||
import org.haxe.lime.HaxeObject;
|
||||
import java.io.InputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class FileUtil extends Extension {
|
||||
private static HaxeObject callback;
|
||||
|
||||
public static void browse(HaxeObject callback) {
|
||||
FileUtil.callback = callback;
|
||||
Intent intent = new Intent()
|
||||
.setType("*/*")
|
||||
.setAction(Intent.ACTION_GET_CONTENT);
|
||||
Extension.mainActivity.startActivityForResult(Intent.createChooser(intent, "Select a file"), 123);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (requestCode == 123 && 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("execute", new Object[]{result});
|
||||
} catch (IOException exception) {
|
||||
Log.e("FileUtil", "", 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user