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
6
Question by cmauceri · Jan 11, 2012 at 02:23 PM · androidplugintutorialscreenshotgallery

Saving Screenshots to Android Gallery

I want to make a screenshot of the Android screen while playing the game by pressing a GUI button and save it to the android's Gallery.

I found a tutorial that details how to save images to the android's gallery using the SDK and java. Here's the link http://developer.android.com/training/camera/photobasics.html

My problem is that I don't understand how to use java with unity. My basic understanding is that I need to either make a library or a plugin. Is that correct? Can you point me to a tutorial that walks me through the process? Or suggest another approach?

Thanks!

Comment
Add comment · Show 3
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 06, 2012 at 01:36 PM 0
Share

Hey, any update here? I'm hitting on the same wall, so some pointers would be great.

avatar image cregox · Dec 12, 2012 at 01:54 PM 0
Share

related: http://answers.unity3d.com/questions/22954/how-to-save-a-picture-take-screenshot-from-a-camer.html - btw did you ever got to a good solution for this? it's quite annoying we can't simply replicate the native android's (or iPhone) native screenshot button behavior.

avatar image DeveshPandey · Feb 10, 2016 at 01:59 PM 0
Share

Capture and Save is a great plugin can be use for this requirement.

http://u3d.as/86U

8 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by cmauceri · Jan 21, 2012 at 04:48 PM

After lots of digging, I've found a solution, but it's made me realize that this post probably should have been three separate questions.

RE: How to use Java Plugins in Unity
The documentation for Unity includes a page describing plugins. Under the Android section, there is a description of how to make a jar file and use AndroidJavaObject to access it in Unity.

RE: How to make and save an Android Screenshot
First, according to this post, androids have problems encoding to PNG. This post provided a link to an alternative jpg encoding.

Second, to view the screenshot from the android file manager, the file path needs to be set using Application.persistentDataPath . More in this post.

RE: Android Gallery
[EDIT] Still working on this... After taking a screenshot, the image does not show up in the gallery regardless of the filepath. However, on rebooting the phone, it does a media scan and automatically adds all image files to the gallery, again regardless of the filepath. There is a post about forcing a media scan, but I have yet to try out the answer

Comment
Add comment · Show 12 · 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 ina · Jan 21, 2012 at 04:52 PM 0
Share

I don't think it's so complicated that you will have to write a Plugin... You just need to have the user specify where their Android Gallery path is (if it's different than the default) ... then use the method to capture the screen @ http://answers.unity3d.com/questions/59944/accessing-the-iphone-or-android-camera-for-screens.html

avatar image ina · Jan 21, 2012 at 04:54 PM 0
Share

/sdcard/DCI$$anonymous$$/camera/ or /sdcard/media/images/

avatar image cmauceri · Jan 21, 2012 at 04:59 PM 0
Share

I came to the conclusion that no plugins were necessary too, but since it was part of my original question, I thought I should answer that part too.

I used your post as a starting point actually about two weeks ago, but at the time I didn't have commenting or upvoting privileges. :) Thanks for the help.

avatar image cmauceri · Jan 21, 2012 at 07:09 PM 0
Share

I think ins$$anonymous$$d of messing with plugins, I'm going to allow the automatic media scan to take care of updating the gallery, and simply inform the user that the image will be available there on reboot.

avatar image marsbear · Aug 15, 2012 at 08:59 PM 0
Share

You have to send an update to the media content provider for the image to show up right after you saved it. I have to dig for the code in one of our projects, do you still need it?

Show more comments
avatar image
12

Answer by tarasfromlviv · Jul 15, 2016 at 02:01 PM

Here is the code that saves Texture2D to Android gallery and does not require to scan media afterwards (picture will appear in gallery instantly)

         private const string MediaStoreImagesMediaClass = "android.provider.MediaStore$Images$Media";

          public static string SaveImageToGallery(Texture2D texture2D, string title, string description)
          {
              using (var mediaClass = new AndroidJavaClass(MediaStoreImagesMediaClass))
              {
                  using (var cr = Activity.Call<AndroidJavaObject>("getContentResolver"))
                  {
                      var image = Texture2DToAndroidBitmap(texture2D);
                      var imageUrl = mediaClass.CallStatic<string>("insertImage", cr, image, title, description);
                      return imageUrl;
                  }
              }
          }
  
          public static AndroidJavaObject Texture2DToAndroidBitmap(Texture2D texture2D)
          {
              byte[] encoded = texture2D.EncodeToPNG();
              using (var bf = new AndroidJavaClass("android.graphics.BitmapFactory"))
              {
                  return bf.CallStatic<AndroidJavaObject>("decodeByteArray", encoded, 0, encoded.Length);
              }
          }



Comment
Add comment · Show 22 · 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 MEDIALLIANCE_2 · Oct 04, 2016 at 09:33 AM 0
Share

Hi tarasfromlviv,

I think you're missing some part of the script: What is $$anonymous$$ediaStoreImages$$anonymous$$ediaClass? Should'nt it be a string like "Android.Provider.$$anonymous$$ediaStore.Images.$$anonymous$$edia"?

How/Where do you declare Activity?

Do you import namespaces?

Thanks.

avatar image tarasfromlviv MEDIALLIANCE_2 · Oct 04, 2016 at 09:49 AM 0
Share

Yeah, sorry I missed some things,

  • $$anonymous$$ediaStoreImages$$anonymous$$ediaClass is private const string $$anonymous$$ediaStoreImages$$anonymous$$ediaClass = "android.provider.$$anonymous$$ediaStore$Images$$$anonymous$$edia";

  • Activity is retrieved as

            public static AndroidJavaObject Activity
             {
                 get
                 {
                     if (_activity == null)
                     {
                         var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                         _activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
                     }
                     return _activity;
                 }
             }
    
    
    
avatar image tarasfromlviv MEDIALLIANCE_2 · Oct 04, 2016 at 09:50 AM 0
Share

This code is actually the part of my asset: https://www.assetstore.unity3d.com/en/#!/content/67473

avatar image MEDIALLIANCE_2 tarasfromlviv · Oct 04, 2016 at 10:07 AM 0
Share

Still, _activity is not defined. Probably private static AndroidJavaObject _activity; Though, if it's part of your paying asset, you probably don't want to share everything. That's understandable. Thanks.

Show more comments
Show more comments
avatar image MEDIALLIANCE_2 · Oct 04, 2016 at 12:01 PM 1
Share

Finally, THAN$$anonymous$$ YOU! This solution is quick and works great. Thumbs up. This IS the answer.

avatar image tarasfromlviv MEDIALLIANCE_2 · Oct 04, 2016 at 12:08 PM 0
Share

You are welcome, btw there is also a free version of my plugin https://www.assetstore.unity3d.com/#!/content/66662 which has lots of good AndroidJavaObject examples.

avatar image pablosj91 tarasfromlviv · Oct 10, 2016 at 07:39 PM 0
Share

where is it going to save the screenshot/image, so far is not working and is not leting me put private const string $$anonymous$$ediaStoreImages$$anonymous$$ediaClass = "android.provider.$$anonymous$$ediaStore$Images$$$anonymous$$edia"; any help??

avatar image lokesh90 · Jan 06, 2017 at 07:26 AM 0
Share

Hello Sir, Can you please tell the logic behind these ...

avatar image ArchVen · Feb 07, 2017 at 12:35 PM 0
Share

Taras, could you help? Your approach is working really good - i was able to save my screenshot to internal Gallery, but i also need to get the real file path for the image (not only Uri).

I've found lots of examples of how to do it in native Java (check below), but how to make it in c# for Unity?

And one more thing - it seems that even you encode the texture2d as PNG, in gallery - it has Jpg. extension and seems to be JPG file?

 public String getRealPathFromURI(Uri contentUri) 
 {
      String[] proj = { $$anonymous$$ediaStore.Audio.$$anonymous$$edia.DATA };
      Cursor cursor = managedQuery(contentUri, proj, null, null, null);
      int column_index = cursor.getColumnIndexOrThrow($$anonymous$$ediaStore.Audio.$$anonymous$$edia.DATA);
      cursor.moveToFirst();
      return cursor.getString(column_index);
 }


avatar image AlundraFlint · Jul 12, 2017 at 10:24 PM 0
Share

do you need the plugin for this code to work? Please help if someone tried. It doesn't seem to do anything for me.

Show more comments
avatar image
1

Answer by zoooom · Apr 05, 2013 at 12:55 PM

This GitHub repo contains functionality that will allow you to save screenshots and images to the gallery on Android and iOS:

https://github.com/ryanw3bb/unity-native-toolkit

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 cregox · Apr 05, 2013 at 01:28 PM 0
Share

It sure looks like. But does it really? Any testimony?

avatar image zoooom · Apr 05, 2013 at 03:45 PM 1
Share

Yep it works for me! iOS + Android.

avatar image cregox · Sep 24, 2013 at 01:49 PM 0
Share

The plugin did the trick to me too. It means there is no simple solution. It offers a compiled library for android and another one for iphone. Awesome plugin! Though this should be default in Unity, which is lame.

avatar image DeveshPandey · Jan 13, 2018 at 12:15 PM 0
Share

Here is cross platform solution to save screenshot to gallery. http://unitydevelopers.blogspot.in/2014/06/capture-and-save-to-camera-roll-and.html

There are 2 versions so you have freedom to select as per your need.

avatar image
0

Answer by marsbear · Aug 16, 2012 at 02:10 PM

I got a partial answer for the 'saving an image in the users gallery on Android' part of the question. That's how we do it in native Android. I will look for a solution from Unity.

 final Bitmap bitmap = INSERTSOMEVIEWHERE.getDrawingCache();
 final File appSzenesFolder;
 try {
     appSzenesFolder = directoryAndStorage.getOrCreatePictureDirectory("NameOfTheImageFolderInTheGallery");
     final Date now = new Date();
     final File file = new File(appSzenesFolder, (now.getTime() / 1000) + ".png");
     file.createNewFile();
 
     final FileOutputStream ostream = new FileOutputStream(file);
     bitmap.compress(CompressFormat.PNG, 100, ostream);
     ostream.close();
 
     // Tell the media scanner about the new file so that it is immediately available to the user.
     MediaScannerConnection.scanFile(this, new String[] { file.toString() }, null, null);
 } catch (IOException e) {
     // Insert error handling here :)
 }

This is pretty much copy and paste from one of our previous projects. I removed some of the error handling (you have to check for the existence of the folder etc).

INSERTSOMEVIEWHERE is some view in Android. This code is basically meant to take a screenshot from a particular view ans save it in the gallery. You can take the root view if you want to take a screenshot of the whole app.

There is that external helper function directoryAndStorage.getOrCreatePictureDirectory I use to create a folder in the users gallery depending on which device we are running on (Some devices don't have an external storage and the Nook does not have the function getExternalStoragePublicDirectory). The helper function is far from complete, you could add support for different Android versions aswell:

 public File getOrCreatePictureDirectory(String directoryName) {
     final File picturesDir;
     if (Build.PRODUCT.equals("NOOKcolor")) {
         // The nook color uses a slightly different path for images and the
         // usual constant for the path does not work
         picturesDir = new File("/mnt/media");
     } else if (Build.PRODUCT.equals("NOOKTablet")) {
         picturesDir = new File("/mnt/media");
     } else {
         picturesDir = Environment
                 .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
     }
     picturesDir.mkdirs();
     File dir = new File(picturesDir, directoryName);
     if (!dir.exists()) {
         Log.i(Config.LOG_TOKEN, "'" + dir
                 + "' does not exist, trying to create ...");
         if (dir.mkdirs() == false) {
             Log.e(Config.LOG_TOKEN, "Failed to create directory '" + dir
                     + "'!");
             return null;
         }
     }
     return dir;
 }

That's it so far. If you have questions, shoot. I'm going to post updates soon.

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 20, 2012 at 11:19 AM 0
Share

Thanks for the code, but I'm capturing a snap from Unity, so I can't use that code to update the sdcard.

I'm actually using [code] sendBroadcast(new Intent(Intent.ACTION_$$anonymous$$EDIA_$$anonymous$$OUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); [/code] which doesn't give me any errors. I just downloaded eclipse with the Android plugin, to compile it to a .jar and to use it as JNI... but yet, I have no idea on what I'm doing, so I haven't achieved anything yet.

avatar image marsbear · Apr 05, 2013 at 03:10 PM 0
Share

$$anonymous$$y code would only be half the solution. You 'just' have to get your image into a Java plugin within your project. I can test how to do that but not before monday or tuesday.

avatar image
0

Answer by alok.kr.029.hotmail · Jul 29, 2014 at 04:32 PM

Use this code work for me

IEnumerator ScreenshotEncode() {

     yield return new WaitForEndOfFrame();
     texture1 = new Texture2D(Screen.width-(int)Left_P.y, Screen.height, TextureFormat.RGB24, false);
     texture1.ReadPixels(new Rect(0+Left_P.y, 0, Screen.width-Left_P.y, Screen.height), 0, 0);
     texture1.Apply();
     for(int i =0 ; i< hiders.Length;i++)
         hiders[i].SetActive(true);
     yield return 0;
       bytes = texture1.EncodeToPNG();

File.WriteAllBytes( "/mnt/sdcard/DCIM/Images/" + "DrawCircuit " +save_Address+".jpg",bytes ); }}

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 DeveshPandey · Jan 13, 2018 at 12:16 PM 0
Share

must check this blog for best solution http://unitydevelopers.blogspot.in/2014/06/capture-and-save-to-camera-roll-and.html

  • 1
  • 2
  • ›

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

32 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 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

Screenshot 0 Answers

Black screenshot of the Unity app from Android 0 Answers

Admob 2.1 in Unity. How to ? 1 Answer

Screenshot is not saving to Gallery and folder 2 Answers

How do I save a screenshot to the gallery on Android Kitkat 4.4.2 or above? 1 Answer


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