- Home /
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
Answer by BBIT-SOLUTIONS · Mar 11, 2020 at 02:39 PM
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?
And did you also have ensured, that you have the necessary permissions?
This code does not throw any errors in the try-catch blog. I allow READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE in android manifest.
I'm not sure how to add OnScanCompletedListener(). I tried a few things but I couldn't run OnScanCompletedListener.
I think you should implement a method in native Java/Android/$$anonymous$$otlin Code, built it as library and add it to your project. Then you can call it in the same way like you are calling e.g. the $$anonymous$$ediaScanner.
And if that works, then you can probably try to call your Unity code again from there, after you received the complete-event.
$$anonymous$$aybe there's an easier way also, but i think that would be the approach, which i would try first.
Your answer
Follow this Question
Related Questions
pick image from android gallery 3 Answers
Save Gif to Photo Gallery from Persistent Path - iOS Android 1 Answer
How to save a image in photo gallery of Android 3 Answers
open another app from ours? 2 Answers