Get Texture from Unity in Android
Get Texture from Unity in Android
Good morning.
I want to use the Android plug-in to save the image to Internal Storage and import the image from Internal Storage.
When you save an image in Unity, the user can access the saved image.
I do not want users to access stored images.
So I tried to create and use Android plugin, but it is not working properly.
I need help.
Android Native Code :
public void WriteToInternalStorage(String name, byte data)
try
FileOutputStream fileOutputStream = context.openFileOutput(name, context.MODE_PRIVATE);
fileOutputStream.write(data);
fileOutputStream.close();
catch(IOException e)
e.printStackTrace();
public byte ReadToInternalStorage(String name)
StringBuffer stringBuffer = new StringBuffer("");
byte buffer = new byte[1024];
int n;
try
FileInputStream fileInputStream = context.openFileInput(name);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
while((line = bufferedReader.readLine()) != null)
stringBuffer.append(line);
catch (IOException e)
e.printStackTrace();
return String.valueOf(stringBuffer).getBytes();
Unity Code :
public void NativeCall_ReadInternalStorage(string name)
byte test = instance.Call<byte>("ReadToInternalStorage", name);
Texture2D tex = new Texture2D(2, 2);
tex.LoadImage(test);
Thumbnail.texture = tex;
The image is not displayed properly.
I do not know what's wrong with the Android native code.
Please help me.
However, my code does not work properly. I posted my code.
– 신두산
Aug 26 at 8:33
What I don't understand is why you're reading an image as a string with
BufferedReader.readLine. I see similar code for Unity on the internet all the time but that's not ok. Load the image as a binary data(byte array) with InputStream then get it in Unity. Note that Unity' Texture2D.LoadImage function only support jpeg and png images. Your C# code seems fine– Programmer
Aug 26 at 8:49
BufferedReader.readLine
InputStream
Texture2D.LoadImage
right. The format of the images I use is PNG, and c # code is fine.
– 신두산
Aug 26 at 9:02
I also know that Android native code is a problem, but I do not know how to fix it. I do not know much about Android native code. So I do not know how to get a byte array using InputStream. I will search for this.
– 신두산
Aug 26 at 9:04
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I am an idiot. Without using UnityPlayer.UnitySendMessage The function can be returned as a byte array and received as a byte array.
– 신두산
Aug 26 at 8:09