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.





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





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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ