I am building an Android application, which will read images from ‘/sdcard/Documents/’ and then display them on a Quad, using unity3D. When I test the app inside Unity in play mode (using images from an external path), the texture of the quad is updated according to the image, but when I build the APK and test it on the device, the quad turns black.
Here’s the code I am using:
public class ShowImage : MonoBehaviour {
[SerializeField] GameObject quad;
int count = 0;
Material quadMaterial;
string extPath;
void Start() {
AndroidJavaClass envClass = new AndroidJavaClass("android.os.Environment");
AndroidJavaObject extStorageDir = envClass.CallStatic<AndroidJavaObject>("getExternalStorageDirectory");
extpath = extStorageDir.Call<string>("getAbsolutepath");
quadMaterial = quad.GetComponent<Renderer>().material;
}
void Update() {
if (File.Exist($"{extPath}/Documents/temp_{count}.png")) {
byte[] imageData = File.ReadAllBytes($"{extPath}/Documents/temp_{count}.png");
Texture2D texture = new Texture2D(2, 2);
// Texture2D texture = new Texture2D(2, 2, TextureFormat.ARG32, true);
texture.LoadImage(imageData);
// ImageConversion.LoadImage(texture, imageData, true);
quadMaterial.mainTexture = texture;
count++;
}
}
}
I put some debug statements also, the image is being read, as imageData.Length
is returning the correct value. Also, I am not getting any errors, I checked the logs using ADB.