public byte[] ReadAllBytes() { byte[] ret = null; #if UNITY_ANDROID_NATIVE if (ByteLength > 0 && _stream != null) { var byteArray = AndroidJNI.NewByteArray(_byteLength); if (byteArray != IntPtr.Zero) { _args[0] = new jvalue() { l = byteArray }; AndroidJNI.CallIntMethod(_stream.GetRawObject(), kMethodPtr_Read, _args); ret = AndroidJNI.FromByteArray(byteArray); AndroidJNI.DeleteLocalRef(byteArray); } } #else var path = string.Format("{0}/{1}", _streamingAssetsPath, FileName); if (File.Exists(path)) { return(File.ReadAllBytes(path)); } #endif //UNITY_ANDROID_NATIVE return(ret); }
public int read(ref byte[] bytes) { JNIFind(); if (_instance == IntPtr.Zero) { Debug.LogError("_instance is not initialized"); return(-1); } if (_jmRead == IntPtr.Zero) { Debug.LogError("_jmRead is not initialized"); return(-1); } IntPtr arg1 = AndroidJNI.ToByteArray(bytes); int result = AndroidJNI.CallIntMethod(_instance, _jmRead, new jvalue[] { new jvalue() { l = arg1 } }); byte[] copy = AndroidJNI.FromByteArray(arg1); Array.Copy(copy, bytes, copy.Length); AndroidJNI.DeleteLocalRef(arg1); return(result); }
private int InternalRead(int length, out byte[] result) { IntPtr resultPtr = AndroidJNI.NewByteArray(length); int offset = 0; int bytesLeft = length; while (bytesLeft > 0) { s_InternalReadArgs[0] = new jvalue() { l = resultPtr }; s_InternalReadArgs[1] = new jvalue() { i = offset }; s_InternalReadArgs[2] = new jvalue() { i = bytesLeft }; int bytesRead = AndroidJNI.CallIntMethod(m_FileStreamRawObject, s_InternalReadMethodId, s_InternalReadArgs); if (bytesRead <= 0) { break; } offset += bytesRead; bytesLeft -= bytesRead; } result = AndroidJNI.FromByteArray(resultPtr); AndroidJNI.DeleteLocalRef(resultPtr); return(offset); }
int Read(AndroidJavaObject javaObject, byte[] buffer, int offset, int count) { var args = new object[] { buffer, offset, count }; IntPtr methodID = AndroidJNIHelper.GetMethodID <int>(javaObject.GetRawClass(), "read", args, false); jvalue[] array = AndroidJNIHelper.CreateJNIArgArray(args); try { var readLen = AndroidJNI.CallIntMethod(javaObject.GetRawObject(), methodID, array); if (readLen > 0) { var temp = AndroidJNI.FromByteArray(array[0].l); Array.Copy(temp, offset, buffer, offset, readLen); } /* * for (var i = offset; i < readLen; ++i) * { * buffer[i] = AndroidJNI.GetByteArrayElement(array[0].l, i); * } */ return(readLen); } finally { AndroidJNIHelper.DeleteJNIArgArray(args, array); } }
public void onRawMessageReceived(AndroidJavaObject senderParticipantId, AndroidJavaObject messageData) { var partcipantId = senderParticipantId.Call <string>("toString"); var bytes = AndroidJNI.FromByteArray(messageData.GetRawObject()); GPGMultiplayerManager.onRealTimeMessageReceived(partcipantId, bytes); }
public override int Read(byte[] buffer, int offset, int count) { lock (_lock) { int ret = 0; IntPtr array = IntPtr.Zero; try { array = AndroidJNI.NewByteArray(count); var method = AndroidJNIHelper.GetMethodID(inputStream.GetRawClass(), "read", "([B)I"); ret = AndroidJNI.CallIntMethod(inputStream.GetRawObject(), method, new[] { new jvalue() { l = array } }); byte[] data = AndroidJNI.FromByteArray(array); Array.Copy(data, 0, buffer, offset, ret); } finally { if (array != IntPtr.Zero) { AndroidJNI.DeleteLocalRef(array); } } return(ret); } }
void INatShare.GetThumbnail(string videoPath, Action <Texture2D> callback, float time) { var thumbnail = natshare.CallStatic <AndroidJavaObject>("getThumbnail", videoPath, time); if (!thumbnail.Call <bool>("isLoaded")) { Debug.LogError("NatShare Error: Failed to get thumbnail for video at path: " + videoPath); callback(null); } var pixelData = AndroidJNI.FromByteArray(thumbnail .Get <AndroidJavaObject>("pixelBuffer") .Call <AndroidJavaObject>("array") .GetRawObject() ); var image = new Texture2D( thumbnail.Get <int>("width"), thumbnail.Get <int>("height"), TextureFormat.RGB565, false ); image.LoadRawTextureData(pixelData); image.Apply(); callback(image); }
private void onPhoto(AndroidJavaObject bitmap, int width, int height, byte orientation) { var pixelBuffer = bitmap.Get <AndroidJavaObject>("mBuffer"); // This is unsafe, but we'll stick with it anyway // `bitmap` is an android.graphics.Bitmap var pixelData = AndroidJNI.FromByteArray(pixelBuffer.GetRawObject()); dispatch.Dispatch(() => { var photo = new Texture2D(width, height, TextureFormat.RGB565, false); photo.LoadRawTextureData(pixelData); photo.Apply(); photoCallback(photo, (Orientation)orientation); }); }
public static string getMemStr(string tag) { IntPtr clazz = AndroidJNI.FindClass("com/tencent/msdk/u3d/DebugMemInfo"); IntPtr staticMethodID = AndroidJNI.GetStaticMethodID(clazz, "getmem_result", "(Ljava/lang/String;)[B"); jvalue[] array = new jvalue[1]; array[0].l = AndroidJNI.NewStringUTF(tag); IntPtr array2 = AndroidJNI.CallStaticObjectMethod(clazz, staticMethodID, array); byte[] array3 = AndroidJNI.FromByteArray(array2); return(Encoding.get_Default().GetString(array3)); }
public static byte[] ConvertByteArray(AndroidJavaObject byteArrayObj) { Debug.Log("ConvertByteArray."); if (byteArrayObj == null) { return(null); } byte[] ret = AndroidJNI.FromByteArray(byteArrayObj.GetRawObject()); AndroidJNI.DeleteLocalRef(byteArrayObj.GetRawObject()); return(ret); }
public byte[] readAllBytes() { var availbleBytes = getFileSize(); var byteArray = AndroidJNI.NewByteArray(availbleBytes); int readCout = AndroidJNI.CallIntMethod(_stream.GetRawObject(), METHOD_read, new[] { new jvalue() { l = byteArray } }); var bytes = AndroidJNI.FromByteArray(byteArray); AndroidJNI.DeleteGlobalRef(byteArray); return(bytes); }
private static byte[] LoadFileFromStreamingAssets(string file) { #if UNITY_EDITOR || UNITY_STANDALONE string assembly = Path.Combine(Application.streamingAssetsPath, file); if (File.Exists(assembly)) { var bytes = File.ReadAllBytes(assembly); return(bytes); } #elif UNITY_ANDROID AndroidJavaClass androidJavaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject androidJavaActivity = null; AndroidJavaObject assetManager = null; AndroidJavaObject inputStream = null; if (androidJavaClass != null) { androidJavaActivity = androidJavaClass.GetStatic <AndroidJavaObject>("currentActivity"); } if (androidJavaActivity != null) { assetManager = androidJavaActivity.Call <AndroidJavaObject>("getAssets"); } if (assetManager != null) { inputStream = assetManager.Call <AndroidJavaObject>("open", file); } if (inputStream != null) { int available = inputStream.Call <int>("available"); System.IntPtr buffer = AndroidJNI.NewByteArray(available); System.IntPtr javaClass = AndroidJNI.FindClass("java/io/InputStream"); System.IntPtr javaMethodID = AndroidJNIHelper.GetMethodID(javaClass, "read", "([B)I"); int read = AndroidJNI.CallIntMethod(inputStream.GetRawObject(), javaMethodID, new[] { new jvalue() { l = buffer } }); byte[] bytes = AndroidJNI.FromByteArray(buffer); AndroidJNI.DeleteLocalRef(buffer); inputStream.Call("close"); inputStream.Dispose(); return(bytes); } #endif return(null); }
private int InternalRead(int length, out byte[] result) { #if UNITY_2019_2_OR_NEWER #pragma warning disable CS0618 #endif IntPtr resultPtr = AndroidJNI.NewByteArray(length); #if UNITY_2019_2_OR_NEWER #pragma warning restore CS0618 #endif int offset = 0; int bytesLeft = length; while (bytesLeft > 0) { s_InternalReadArgs[0] = new jvalue() { l = resultPtr }; s_InternalReadArgs[1] = new jvalue() { i = offset }; s_InternalReadArgs[2] = new jvalue() { i = bytesLeft }; int bytesRead = AndroidJNI.CallIntMethod(m_FileStreamRawObject, s_InternalReadMethodId, s_InternalReadArgs); if (bytesRead <= 0) { break; } offset += bytesRead; bytesLeft -= bytesRead; } #if UNITY_2019_2_OR_NEWER #pragma warning disable CS0618 #endif result = AndroidJNI.FromByteArray(resultPtr); #if UNITY_2019_2_OR_NEWER #pragma warning restore CS0618 #endif AndroidJNI.DeleteLocalRef(resultPtr); return(offset); }
Texture2D INatShare.GetThumbnail(string videoPath, float time) { using (var thumbnail = natshare.Call <AndroidJavaObject>("getThumbnail", videoPath, time)) { if (!thumbnail.Call <bool>("isLoaded")) { Debug.LogError("NatShare Error: Failed to get thumbnail for video at path: " + videoPath); return(null); } var width = thumbnail.Get <int>("width"); var height = thumbnail.Get <int>("height"); using (var pixelBuffer = thumbnail.Get <AndroidJavaObject>("pixelBuffer")) using (var array = pixelBuffer.Call <AndroidJavaObject>("array")) { var pixelData = AndroidJNI.FromByteArray(array.GetRawObject()); var image = new Texture2D(width, height, TextureFormat.RGB565, false); // Weird texture format IMO image.LoadRawTextureData(pixelData); image.Apply(); return(image); } } }
/// <summary> /// Call from java get asset file bytes and convert to string /// </summary> /// <param name="path"></param> /// <returns></returns> public static string GetAssetString(string path) { #if !KENGINE_DLL && UNITY_ANDROID #if UNITY_2017 var clsPtr = AndroidJNI.FindClass("com/github/KEngine/AndroidHelper"); var methondPtr_getAssetBytes = AndroidJNIHelper.GetMethodID(clsPtr, "getAssetBytes", "(Ljava/lang/String;)[B", true); var arg1 = new object[] { path }; var args = AndroidJNIHelper.CreateJNIArgArray(arg1); var byteArray = AndroidJNI.CallStaticObjectMethod(clsPtr, methondPtr_getAssetBytes, args); var bytes = AndroidJNI.FromByteArray(byteArray); AndroidJNIHelper.DeleteJNIArgArray(arg1, args); AndroidJNI.DeleteLocalRef(byteArray); AndroidJNI.DeleteLocalRef(clsPtr); return(bytes); #else return(AndroidHelper.CallStatic <string>("getAssetString", path)); #endif #else ErrorNotSupport(); return(null); #endif }
public static ulong URLClientMovePendingResponseContent(uint connectionID, byte[] dst, ulong dstLength) { ulong length; #if UNITY_EDITOR || UNITY_STANDALONE_OSX || UNITY_IPHONE GCHandle pinnedArray = GCHandle.Alloc(dst, GCHandleType.Pinned); IntPtr ptrDst = pinnedArray.AddrOfPinnedObject(); length = _URLClientMovePendingResponseContent(connectionID, ptrDst, dstLength); pinnedArray.Free(); #elif UNITY_ANDROID IntPtr rawClass = UnityURLClientBindingInstance.GetRawClass(); IntPtr rawObject = UnityURLClientBindingInstance.GetRawObject(); IntPtr methodPtr = AndroidJNI.GetMethodID(rawClass, "movePendingResponseContent", "(I[BJ)J"); IntPtr v2 = AndroidJNI.ToByteArray(dst); jvalue j1 = new jvalue(); j1.i = (int)connectionID; jvalue j2 = new jvalue(); j2.l = v2; jvalue j3 = new jvalue(); j3.j = (long)dstLength; length = (ulong)AndroidJNI.CallLongMethod(rawObject, methodPtr, new jvalue[] { j1, j2, j3 }); if (dst != null) { byte[] resultDst = AndroidJNI.FromByteArray(v2); for (ulong n = 0; n < length; ++n) { dst[n] = resultDst[n]; } } AndroidJNI.DeleteLocalRef(v2); #endif return(length); }
public byte[] GetBytes(string path) { if (!isInit) { var _helper = new AndroidJavaClass("unity.tools.ReadAsset"); using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { object jo = jc.GetStatic <AndroidJavaObject>("currentActivity"); _helper.CallStatic("init", jo); } clazzPtr = AndroidJNI.FindClass("unity/tools/ReadAsset"); methodPtr1 = AndroidJNI.GetStaticMethodID(clazzPtr, "isFileExists", "(Ljava/lang/String;)Z"); methodPtr2 = AndroidJNI.GetStaticMethodID(clazzPtr, "readFile", "(Ljava/lang/String;)[B"); isInit = true; } byte[] data = null; object[] objs = new object[] { path }; jvalue[] jvs = AndroidJNIHelper.CreateJNIArgArray(objs); IntPtr dataPtr = AndroidJNI.CallStaticObjectMethod(clazzPtr, methodPtr2, jvs); if (dataPtr != IntPtr.Zero) { data = AndroidJNI.FromByteArray(dataPtr); AndroidJNI.DeleteLocalRef(dataPtr); } AndroidJNIHelper.DeleteJNIArgArray(objs, jvs); if (data == null) { Debug.LogError("android Load package file error:" + path); } return(data); }
public override byte[] Load(string name) { //取得应用的Activity //LogSystem.Info(bundlename); //从Activity取得AssetManager实例 AndroidJavaObject assetManager = ActivityJO.Call <AndroidJavaObject>("getAssets"); //打开文件流 AndroidJavaObject stream = assetManager.Call <AndroidJavaObject>("open", name); //获取文件长度 int availableBytes = stream.Call <int>("available"); //取得InputStream.read的MethodID IntPtr clsPtr = AndroidJNI.FindClass("java/io/InputStream"); IntPtr METHOD_read = AndroidJNIHelper.GetMethodID(clsPtr, "read", "([B)I"); //IntPtr METHOD_read = AndroidJNIHelper.GetMethodID(clsPtr, "read"); //申请一个Java ByteArray对象句柄 IntPtr byteArray = AndroidJNI.NewByteArray(availableBytes); //调用方法 int readCount = AndroidJNI.CallIntMethod(stream.GetRawObject(), METHOD_read, new[] { new jvalue() { l = byteArray } }); //从Java ByteArray中得到C# byte数组 byte[] bytes = AndroidJNI.FromByteArray(byteArray); //删除Java ByteArray对象句柄 AndroidJNI.DeleteLocalRef(clsPtr); AndroidJNI.DeleteLocalRef(byteArray); //关闭文件流 stream.Call("close"); stream.Dispose(); //返回结果 return(bytes); }
public static byte[] GetByteArray(ByteArray byteArray) { byte[] data = AndroidJNI.FromByteArray(byteArray.Array); return(data); }
[Preserve] private void onPhoto(AndroidJavaObject photo) { parent.OnPhoto(AndroidJNI.FromByteArray(photo.Get <AndroidJavaObject>(@"pixelBuffer").GetRawObject()), photo.Get <int>(@"width"), photo.Get <int>(@"height")); }
public byte[] loadExternalFileData(string fileName) { using (var javaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { AndroidJavaObject activity = null; AndroidJavaObject stream = null; AndroidJavaObject assetManager = null; try { //取得应用的Activity activity = javaClass.GetStatic <AndroidJavaObject>("currentActivity"); //从Activity取得AssetManager实例 assetManager = activity.Call <AndroidJavaObject>("getAssets"); //打开文件流 stream = assetManager.Call <AndroidJavaObject>("open", fileName); //获取文件长度 var availableBytes = stream.Call <int>("available"); byte[] bytes = null; if (availableBytes > 0) { //取得InputStream.read的MethodID var clsPtr = AndroidJNI.FindClass("java.io.InputStream"); var METHOD_read = AndroidJNIHelper.GetMethodID(clsPtr, "read", "([B)I"); //申请一个Java ByteArray对象句柄 var byteArray = AndroidJNI.NewByteArray(availableBytes); //调用方法 int readCount = AndroidJNI.CallIntMethod(stream.GetRawObject(), METHOD_read, new[] { new jvalue() { l = byteArray } }); //从Java ByteArray中得到C# byte数组 bytes = AndroidJNI.FromByteArray(byteArray); //删除Java ByteArray对象句柄 AndroidJNI.DeleteLocalRef(byteArray); } //关闭文件流 if (stream != null) { stream.Call("close"); stream.Dispose(); } if (assetManager != null) { assetManager.Dispose(); } if (activity != null) { activity.Dispose(); } //返回结果 return(bytes); } catch (System.Exception ex) { //关闭文件流 if (stream != null) { stream.Call("close"); stream.Dispose(); } if (assetManager != null) { assetManager.Dispose(); } if (activity != null) { activity.Dispose(); } GameDebug.LogError("loadExternalFileData error: " + ex.Message); } } return(null); }
protected static void GetFileInformation(String name, ref long length, ref IntPtr pDataUnManaged) { #if UNITY_ANDROID long start; Int32 fd; IntPtr cls_Activity = (IntPtr)AndroidJNI.FindClass("com/unity3d/player/UnityPlayer"); IntPtr fid_Activity = AndroidJNI.GetStaticFieldID(cls_Activity, "currentActivity", "Landroid/app/Activity;"); IntPtr obj_Activity = AndroidJNI.GetStaticObjectField(cls_Activity, fid_Activity); IntPtr obj_cls = AndroidJNI.GetObjectClass(obj_Activity); IntPtr asset_func = AndroidJNI.GetMethodID(obj_cls, "getAssets", "()Landroid/content/res/AssetManager;"); IntPtr assetManager = AndroidJNI.CallObjectMethod(obj_Activity, asset_func, new jvalue[2]); IntPtr assetManagerClass = AndroidJNI.GetObjectClass(assetManager); IntPtr openFd = AndroidJNI.GetMethodID(assetManagerClass, "openFd", "(Ljava/lang/String;)Landroid/content/res/AssetFileDescriptor;"); jvalue[] param_array2 = new jvalue[2]; jvalue param = new jvalue(); param.l = AndroidJNI.NewStringUTF(name); param_array2[0] = param; IntPtr jfd; try { jfd = AndroidJNI.CallObjectMethod(assetManager, openFd, param_array2); IntPtr assetFdClass = AndroidJNI.GetObjectClass(jfd); IntPtr getParcelFd = AndroidJNI.GetMethodID(assetFdClass, "getParcelFileDescriptor", "()Landroid/os/ParcelFileDescriptor;"); IntPtr getStartOffset = AndroidJNI.GetMethodID(assetFdClass, "getStartOffset", "()J"); IntPtr getLength = AndroidJNI.GetMethodID(assetFdClass, "getLength", "()J"); start = AndroidJNI.CallLongMethod(jfd, getStartOffset, new jvalue[2]); length = AndroidJNI.CallLongMethod(jfd, getLength, new jvalue[2]); IntPtr fileInputStreamId = AndroidJNI.GetMethodID(assetFdClass, "createInputStream", "()Ljava/io/FileInputStream;"); IntPtr fileInputStream = AndroidJNI.CallObjectMethod(jfd, fileInputStreamId, new jvalue[2]); IntPtr fileInputStreamClass = AndroidJNI.GetObjectClass(fileInputStream); // Method signatures:newbytear B: byte, Z: boolean IntPtr read = AndroidJNI.GetMethodID(fileInputStreamClass, "read", "([BII)I"); jvalue[] param_array = new jvalue[3]; jvalue param1 = new jvalue(); IntPtr pData = AndroidJNI.NewByteArray((int)(length)); param1.l = pData; jvalue param2 = new jvalue(); param2.i = 0; jvalue param3 = new jvalue(); param3.i = (int)(length); param_array[0] = param1; param_array[1] = param2; param_array[2] = param3; int numBytesRead = AndroidJNI.CallIntMethod(fileInputStream, read, param_array); UnityEngine.Debug.Log("Bytes Read = " + numBytesRead); Byte[] pDataManaged = AndroidJNI.FromByteArray(pData); pDataUnManaged = Marshal.AllocCoTaskMem((int)length); Marshal.Copy(pDataManaged, 0, pDataUnManaged, (int)length); jfd = AndroidJNI.CallObjectMethod(jfd, getParcelFd, new jvalue[2]); IntPtr parcelFdClass = AndroidJNI.GetObjectClass(jfd); jvalue[] param_array3 = new jvalue[2]; IntPtr getFd = AndroidJNI.GetMethodID(parcelFdClass, "getFileDescriptor", "()Ljava/io/FileDescriptor;"); jfd = AndroidJNI.CallObjectMethod(jfd, getFd, param_array3); IntPtr fdClass = AndroidJNI.GetObjectClass(jfd); IntPtr descriptor = AndroidJNI.GetFieldID(fdClass, "descriptor", "I"); fd = AndroidJNI.GetIntField(jfd, descriptor); } catch (IOException ex) { UnityEngine.Debug.Log("IO Exception: Failed to load swf file"); } #endif return; }