Android: How to refresh the gallery using Unity?

I am trying to update the gallery after getting a Screenshot from my app. I tried different solutions but none of them worked for me. I’m developing apps for Android(Android 8.0+) on Unity.

1. Solution;

AndroidJavaClass jcUnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject objActivity = jcUnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaClass classUri = new AndroidJavaClass("android.net.Uri");
AndroidJavaObject objIntent = new AndroidJavaObject("android.content.Intent", new object[2]{ 
"android.intent.action.ACTION_MEDIA_SCANNER_SCAN_FILE", classUri.CallStatic<AndroidJavaObject> 
("parse", myFolderLocation) });
objActivity.Call("sendBroadcast", objIntent);

I tried the following options;

  • ACTION_MEDIA_SCANNER_SCAN_FILE
  • MEDIA_SCANNER_SCAN_FILE
  • ACTION_MEDIA_MOUNTED
  • MEDIA_MOUNTED

2. Solution;

using (AndroidJavaClass jcUnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
using (AndroidJavaObject joActivity = jcUnityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
using (AndroidJavaObject joContext = joActivity.Call<AndroidJavaObject>("getApplicationContext"))
using (AndroidJavaClass jcMediaScannerConnection = new AndroidJavaClass("android.media.MediaScannerConnection"))
using (AndroidJavaClass jcEnvironment = new AndroidJavaClass("android.os.Environment"))
using (AndroidJavaObject joExDir = jcEnvironment.CallStatic<AndroidJavaObject>("getStorageDirectory"))
{
       jcMediaScannerConnection.CallStatic("scanFile", joContext, new string[] { myFolderLocation }, null, null);
}

Thank you for your help

Do you get any errors or logs?

According to this thread on stackoverflow your 2. Solution should actually work. Maybe you should add an OnScanCompletedListener() as 4th parameter, so that you are able to receive the scanComplete-event?