I’m making a screen capture (through custom bmp save function since Application.CaptureScreenshot doesn’t work on 3.4.0f5), resulting bitmap is stored on the sdcard, if I reboot the captures are correctly displayed.
I’ve found some android.media.MediaScannerConnection / Instant things, but I didn’t manage to use it through AndroidJavaClass.
Below is a code sample that can be executed by calling the following line…
StartCoroutine(TakeScreenshot());
private IEnumerator TakeScreenshot()
{
yield return new WaitForEndOfFrame();
//INITIAL SETUP
string myFilename = "myScreenshot.png";
string myDefaultLocation = Application.persistentDataPath + "/" + myFilename;
//EXAMPLE OF DIRECTLY ACCESSING THE Camera FOLDER OF THE GALLERY
//string myFolderLocation = "/storage/emulated/0/DCIM/Camera/";
//EXAMPLE OF BACKING INTO THE Camera FOLDER OF THE GALLERY
//string myFolderLocation = Application.persistentDataPath + "/../../../../DCIM/Camera/";
//EXAMPLE OF DIRECTLY ACCESSING A CUSTOM FOLDER OF THE GALLERY
string myFolderLocation = "/storage/emulated/0/DCIM/MyFolder/";
string myScreenshotLocation = myFolderLocation + myFilename;
//ENSURE THAT FOLDER LOCATION EXISTS
if(!System.IO.Directory.Exists(myFolderLocation)){
System.IO.Directory.CreateDirectory(myFolderLocation);
}
//TAKE THE SCREENSHOT AND AUTOMATICALLY SAVE IT TO THE DEFAULT LOCATION.
Application.CaptureScreenshot(myFilename);
//MOVE THE SCREENSHOT WHERE WE WANT IT TO BE STORED
System.IO.File.Move(myDefaultLocation, myScreenshotLocation);
//REFRESHING THE ANDROID PHONE PHOTO GALLERY IS BEGUN
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);
//REFRESHING THE ANDROID PHONE PHOTO GALLERY IS COMPLETE
//AUTO LAUNCH/VIEW THE SCREENSHOT IN THE PHOTO GALLERY
Application.OpenURL(myScreenshotLocation);
//AFTERWARDS IF YOU MANUALLY GO TO YOUR PHOTO GALLERY,
//YOU WILL SEE THE FOLDER WE CREATED CALLED "myFolder"
}
Hey - I just had to figure out how to do this as well and figured I’d share the gist of my solution. First I had to extend the UnityPlayerActivity - mainly so I’d have a proper context to send with the refresh command. So in the OnCreate, I store off the context like so (“me” just being a static reference to my subclass):
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
me = this;
}
Then I have another function in my activity subclass for the refresh:
I have a separate java plugin that just calls that static method, and I reference that plugin through Unity with an AndroidJavaClass object. It’s a bit convoluted and I could probably clean it up, but it’s working, so I’m gonna leave it as is for now.
Hope that’s enough to get you pointed in the right direction.