- Home /
How to refresh android gallery
I record a video in unity, I move this video to the DCIM folder in the device, but I can only see the video when I restart the device. I guess that when I turn on my phone it scann the memory, is there a posibility to make that scann affter the video is moved?. I saw post about using unity pluggins and using the "System.IO" but I cant figure it out how to make it work. this looks like doing something, but its not working.
//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
this is also a code from this question but it doesnt work for me http://answers.unity3d.com/questions/200173/android-how-to-refresh-the-gallery-.html
Answer by Hazzanger · Sep 26, 2016 at 12:20 AM
NOTE: This plugin will refresh the gallery and open it. It is assumed you have already saved the screenshot or image prior to this.
Accessing the gallery from Unity alone can't be done, but using an IDE medium like android studio or Eclipse can. Unity just doesn't have the correct API. So if you open up android studio and create a plugin, there's very little you have to do in unity. Use this link to see a tutorial about making plugins:
http://www.thegamecontriver.com/2015/04/android-plugin-unity-android-studio.html
Now, you can make your own plugin, or just use one that I made:
https://drive.google.com/file/d/0B_oGNe3B4oysNGJuOEN0TGZYNEU/view?usp=sharing
I used the package name of com.vexstarstruck.unity. I don't know if it is necessary for you to do the same. If so, you could just make your own plugin. I highly recommend you learn how to make your own plugins as it will be extremely useful to use the native functions android provides.
Copy this jar file into your Assets/Plugins/Android folder. Then in Unity, when you want to call this plugin, add this code to any script after you save the image:
AndroidJavaClass unity = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject> ("currentActivity");
currentActivity.Call ("addImageGallery", myScreenshotLocation);
The reason it may not have worked for you is because you did not have the correct plugin naming, function, or package name. Either way, anyone can contact me if you need clarification or extra help.
this means that for IOS I have to use a pluggin created in xcode? thanks for the suggestion and the tutorial links, you rock, I will find a way to make pluggins. :D
Hi @Hazzanger i tried your plugin but it was not working for me ,can you give me any idea to do this. Thanks in advance
Answer by justinMochaRax · Mar 13, 2017 at 04:26 PM
Just change android.intent.action.MEDIA_MOUNTED
into android.intent.action.MEDIA_SCANNER_SCAN_FILE
, because ACTION_MEDIA_MOUNTED got locked in KitKat. Thanksfully ACTION_MEDIA_SCANNER_SCAN_FILE still works.
Hope this will help.
Answer by dansyo · Mar 17, 2017 at 10:06 AM
MediaScannerConnection class scans for new files and directories on to gallery .. Check this post for more information http://whats-online.info/science-and-tutorials/135/how-to-save-an-image-to-gallery-in-android-programmatically/
Your answer
Follow this Question
Related Questions
Android: How to refresh the gallery ? 4 Answers
Save Screenshot and Show it in Gallery (iOS & Android) 3 Answers
[Edited]Access photos from gallery in application (Android) 1 Answer
Screenshot 0 Answers
Saving screencap to android gallery 0 Answers