Android: How to refresh the gallery ?

Hello,

How to force the refresh of the Android gallery ?

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.

Thanks to user @shinichi88 in this forum thread, I figured it out!

First make sure you include this at the top of your script…

using System.IO;

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:

public static void GalleryRefresh()
{
    me.sendBroadcast(new Intent(Intent.ACTION\_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}

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.

Got it, I’ll go this way.

Many thanks!

This worked for me -

You have to poll the directory you’re saving the screenshot to after writing - seems to force a gallery refresh. My definitely non-expert code here:

File.WriteAllBytes(Application.persistentDataPath + "/../../../../DCIM/Camera/" + "Screenshot.jpg", imageArray);
					
fileNames = System.IO.Directory.GetFiles(Application.persistentDataPath + "/../../../../DCIM/Camera/");

Hello Guys,

You should use this wonderful plugin with full support.

There is a library to refresh android gallery.