Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by skarab · Dec 30, 2011 at 08:32 PM · androidrefreshcapturegallery

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
5

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"
     }
 


Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image WavyRancheros · Jul 16, 2015 at 01:17 PM 0
Share

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

avatar image ash_05 · Dec 21, 2015 at 09:34 AM 0
Share

@$$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

avatar image jofussnc ash_05 · Mar 04, 2016 at 12:10 PM 0
Share

@$$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.

avatar image Kurius jofussnc · Mar 04, 2016 at 05:17 PM 0
Share

I only tested it on Samsung devices and it works fine on them.

Show more comments
avatar image AnnieN · Sep 26, 2019 at 09:23 AM 0
Share

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.

avatar image
2

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Miyavi · Aug 21, 2012 at 11:04 AM 0
Share

Could you share that project? I'm unable to set JNI nor Plugins correclty. It's driving me mad.

avatar image cregox · Dec 12, 2012 at 09:45 PM 0
Share

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

avatar image
0

Answer by skarab · Jan 04, 2012 at 01:15 AM

Got it, I'll go this way.

Many thanks!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cregox · Dec 12, 2012 at 06:02 PM 0
Share

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.

avatar image
0

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/");
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hizral · Dec 10, 2012 at 04:16 AM 0
Share

How do you do this? what is "filenames"?? how do you setup the script do you used c# or unityscript?

avatar image cregox · Dec 12, 2012 at 06:02 PM 0
Share

`Directory.GetFiles` doesn't work for me

avatar image el-santia93 · Dec 09, 2013 at 05:30 PM 0
Share

anybody finally figure out how to do this.....am stuck on something similar

avatar image cregox · Dec 09, 2013 at 08:03 PM 0
Share

@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

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges