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("");
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;


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(""))
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?