Android的JNI JavaScript綁定是否有效地將TypedArray / ArrayBuffer作為數組傳遞給Java ?

[英]Does Android's JNI JavaScript binding efficiently pass TypedArray / ArrayBuffer to Java as an array?


In the Android JNI binding, you can expose a Java method such as method(int[] intArray) {} to JavaScript, pass it a JavaScript array, and expect the binding to convert that JavaScript array to int[]. Does Android have the same handling for e.g. Uint8Array()?

在Android JNI綁定中,您可以公開Java方法,例如方法(int[] intArray){}到JavaScript,傳遞一個JavaScript數組,並期望綁定將JavaScript數組轉換為int[]。Android對Uint8Array()有同樣的處理嗎?

1 个解决方案

#1


5  

The JavaScript engine that runs in Android's WebView does not support Uint8Arrays, or any of the other typed arrays (as of 2.3.3).

在Android的WebView中運行的JavaScript引擎不支持uint8array或任何其他類型的數組(如2.3.3)。

EDIT: I did some more testing with the simulators and I have mixed things to report.

編輯:我用模擬器做了更多的測試,我有混合的東西要報告。

On the plus side, the JavaScript engine in Android 3.x's WebView does support typed arrays, with the exception of Float64Array.

從好的方面來看,Android 3中的JavaScript引擎。x的WebView支持類型化數組,除了Float64Array。

On the minus side, the JNI interface to Java via WebView.addJavascriptInterface() does not convert the types at all. This is the code I used to test it:

另一方面,通過WebView.addJavascriptInterface()到Java的JNI接口根本沒有轉換類型。這是我用來測試它的代碼:

var u8arr = new Uint8Array(4);
u8arr.set([2, 3, 5, 7]);
android.log(u8arr);

And the Java function for android.log() looks basically like this:

而android.log()的Java函數基本上是這樣的:

public void log(final int[] data) {
    final String dataInfo = (data == null) ? "NULL" : "[" + data.length + "]";
    Log.d("JsJni", "data=" + dataInfo);
    /* ... */
}

When I called android.log with standard Array() type JavaScript object, I'd get the results one would expect (the array bound to data, type conversion, etc...) When android.log was called with the Uint8Array object (or any of the other typed arrays, for that matter, including Int8Array, Int16Array, and Int32Array), data is null.

當我打電話給android。使用標准數組()類型的JavaScript對象進行日志記錄,我將得到預期的結果(數組綁定到數據、類型轉換等等)。當安卓。使用Uint8Array對象(或任何其他類型化數組,包括Int8Array、Int16Array和Int32Array)調用日志,數據為空。

It's the same between Android 3.0 and 3.2

Android 3.0和3.2也是一樣的


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/08/09/7255c3bbb06f7b2a1284616bcd9197e4.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com