0

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.

신두산
  • 1
  • 2
  • 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 '18 at 08:09
  • However, my code does not work properly. I posted my code. – 신두산 Aug 26 '18 at 08:33
  • 1
    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 '18 at 08:49
  • right. The format of the images I use is PNG, and c # code is fine. – 신두산 Aug 26 '18 at 09: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 '18 at 09:04
  • try { InputStream fileInputStream = context.openFileInput(name); while((n = fileInputStream.read()) != -1) { byteBuffer.write(buffer, 0, n); } } This code also does not work properly. – 신두산 Aug 26 '18 at 09:21
  • See [this](https://stackoverflow.com/questions/10039672/android-how-to-read-file-in-bytes) post for how to read data as byte array. Many answers that doesn't involve reading as string. Do not forget to enable external storage from Unity's Build Setting which adds the required permission. – Programmer Aug 26 '18 at 09:49
  • Unity has received the Android Internal Storage Path and solved Write and Read on that Path by Unity. – 신두산 Aug 27 '18 at 03:47

0 Answers0