I am trying to save my screenshot in the image gallery of my device. It works great, but it will not appear in the gallery.
This is what I do
selfie= new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
selfie.ReadPixels(new Rect(0, 0,Screen.width, Screen.height), 0, 0,false);
selfie.Apply ();
byte[] bytes = selfie.EncodeToPNG();
string filename = "Screenshot.png";
fileLocation = Path.Combine( Application.persistentDataPath, filename );
File.WriteAllBytes(fileLocation, bytes );
string myFolderLocation = "/mnt/sdcard/DCIM/Camera/";
myScreenshotLocation = myFolderLocation + filename;
System.IO.File.Move(fileLocation, myScreenshotLocation);
Saves the image in the right place. If I look at the SD card, it is. But he does not appear in the gallery. So I tried this after File.move to update the gallery.
AndroidJavaClass classPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject objActivity = classPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaClass classUri = new AndroidJavaClass("android.net.Uri");
AndroidJavaObject objIntent = new AndroidJavaObject("android.content.Intent", new object[2]{"android.intent.action.MEDIA_MOUNTED", classUri.CallStatic<AndroidJavaObject>("parse", "file://" + myScreenshotLocation)});
objActivity.Call ("sendBroadcast", objIntent);
But it does nothing. I work with the latest version of Unity version 5.3 and should work on all Android and ios devices. Any ideas how to make this work and appear anywhere in the gallery?
Thanks!
Jenny source
share