- Home /
Can you save a texture to android with orientation from unity?
So on android, when I'm loading images into my app from the gallery I use a native plugin which gives me the file's location and orientation. Shortly after loading the image into a Texture2D I'd like to save it off again, except in a different location but with the same orientation.
Is there anyway to do this directly from unity?
Here's the code I use for saving the image currently:
void SaveTextureToFile(Texture2D texture, string fileName)
{
byte[] bytes=texture.EncodeToPNG();
FileStream file = File.Open(Application.persistentDataPath + Path.PathSeparator + fileName, FileMode.OpenOrCreate);
var binary= new BinaryWriter(file);
binary.Write(bytes);
file.Close();
}
did you solve this? I'm interested in loading the images with different orientations :/
Answer by Geebles · Sep 10, 2015 at 12:33 PM
So I didn't manage to find a direct solution provided by unity, but I did manage to solve the problem. Here are the steps I took:
First I made a Texture2D in Unity the same size as the image I wanted to load. (After scaling & rotation)
Once you've done that you can grab the texture ID from it using GetNativeTextureID.
Now use some native code to load, scale, rotate and bind your desired image to the textureID.
The last bullet point is very platform dependent. in my case (Android) I used the BitmapFactory class to initially load the file and then the Bitmap class to apply my scale/rotation matrix, Then I used OpenGL to bind the image to the textureID.
I hope that helps.