- Home /
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.
Answer by Kurius · Feb 04, 2015 at 06:34 AM
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"
}
I'm getting an error message with the intent: AndroidJavaException: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.$$anonymous$$EDIA_$$anonymous$$OUNTED from pid=4109, uid=10167
@$$anonymous$$urius : I followed the same procedure
The photo is getting stored in the folder "myFolder" but the photo is not updated in the Gallery folder.
This happens in the tablet,but working fine only in Samsung phone not other phones
Can you please help me solving it
@$$anonymous$$urius have you solve this update problem? I have the same problem, i can get steored the photo, but it doesnt update to the gallery. ¿Can you help me? Thank you.
I only tested it on Samsung devices and it works fine on them.
Thanks. In my case, your code works well with some update: [1]- Change code (the old code is commented)
// Application.CaptureScreenshot(myFilename);
ScreenCapture.CaptureScreenshot(fileName); //Application.CaptureScreenshot() is deprecated
yield return new WaitForSeconds(1f); // Wait for capturing complete into file
[2] - Change code
//AndroidJavaObject objIntent = new AndroidJavaObject("android.content.Intent", new object[2]{"android.intent.action.$$anonymous$$EDIA_$$anonymous$$OUNTED", classUri.CallStatic<AndroidJavaObject>("parse", "file://" + myScreenshotLocation)});
AndroidJavaObject objIntent = new AndroidJavaObject("android.content.Intent", new object[2]{"android.intent.action.$$anonymous$$EDIA_SCANNER_SCAN_FILE", classUri.CallStatic<AndroidJavaObject>("parse", "file://" + myScreenshotLocation)});
Cant use $$anonymous$$EDIA_$$anonymous$$OUNTED anymore, use $$anonymous$$EDIA_SCANNER_SCAN_FILE ins$$anonymous$$d. Not ACTION_$$anonymous$$EDIA_SCANNER_SCAN_FILE, just $$anonymous$$EDIA_SCANNER_SCAN_FILE
[3] - Note of folder-path This path is O$$anonymous$$:
string myFolderLocation = "/storage/emulated/0/DCI$$anonymous$$/$$anonymous$$yFolder/";
string myScreenshotLocation = myFolderLocation + myFilename;
But if you stated it as below, with persistentDataPath, the gallery wont be updated:
string myFolderLocation = Application.persistentDataPath + "/" + $$anonymous$$yFolder+ "/";
Tested on Android 6, Sony z3. Hope it helps.
Answer by BuzzJive_Legacy · Jan 03, 2012 at 11:44 PM
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.
Could you share that project? I'm unable to set JNI nor Plugins correclty. It's driving me mad.
I wouldn't call this "the right direction", but yeah... It does point in a direction that seems to solve the issue in the worst (and possibly still only) way: http://docs.unity3d.com/Documentation/$$anonymous$$anual/PluginsForAndroid.html
Answer by skarab · Jan 04, 2012 at 01:15 AM
Got it, I'll go this way.
Many thanks!
Not an answer, and not even showing which way... You should move this as comment on the answer you think worked, vote it up and accept it.
Answer by Shaolin23 · Nov 13, 2012 at 05:34 PM
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/");
How do you do this? what is "filenames"?? how do you setup the script do you used c# or unityscript?
anybody finally figure out how to do this.....am stuck on something similar
@el-santia93 somebody did figure it out, and you'd need to deal with DLLs. I don't know any details, except it is just few lines of code: https://www.assetstore.unity3d.com/#/content/7827
Answer by DeveshPandey · Jun 30, 2014 at 04:04 PM
Hello Guys,
You should use this wonderful plugin with full support.
http://u3d.as/content/devesh-pandey/capture-and-save/86U
There is a library to refresh android gallery.
Your answer
Follow this Question
Related Questions
How to refresh android gallery 3 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