I have an Android app built in Unity where I'm trying to iterate over and display N images stored in the user's android device. The idea is to read their images into Textures and map them onto quads in my Unity scene.
To simplify the problem I'm simply attempting to get hold of a single file that I know 100% exists on my device's photo gallery in the following directory "/storage/emulated/0/DCIM/Camera/":
string path = "/storage/emulated/0/DCIM/Camera/20161019_142127.jpg";
WWW www = new WWW("file://" + path);
yield return www;
if (string.IsNullOrEmpty(www.error))
{
m_material.mainTexture = www.texture;
Debug.Log("No Error, texture successfully set");
}
else
{
Debug.Log("Error returned = " + www.error);
}
But I keep getting the following error message, suggesting that it failed to load the file:
Error returned = java.lang.NullPointerException: Attempt to invoke virtual method 'int java.io.InputStream.read(byte[])' on a null object reference
Would someone please be able to help me understand how to access this file correctly and consequently iterate over N image files in such a directory?
Thanks in advance!
Maybe you can try using File.ReadAllBytes
from System.IO
:
Texture2D texture = new Texture2D(2, 2, TextureFormat.ARGB32, false);
byte[] fileByteData = File.ReadAllBytes(path);
texture.LoadImage(fileByteData);
The texture will automatically resize when calling LoadImage
.