- Home /
How can I take screenshot in an unity application and save it to device's gallery (for android and ios both)?
I have done with this screenshot but it does not save into mobile's gallery application but it is there where the apk build is being installed.
Any help would be appreciated. Thanks in advance..!!
Answer by ShraddhaP · Dec 05, 2017 at 10:04 AM
IEnumerator CRSaveScreenshot()
{
yield return new WaitForEndOfFrame();
// string TwoStepScreenshotPath = MobileNativeShare.SaveScreenshot("Screenshot" + System.DateTime.Now.Hour + System.DateTime.Now.Minute + System.DateTime.Now.Second);
// Debug.Log("A new screenshot was saved at " + TwoStepScreenshotPath);
string myFileName = "Screenshot" + System.DateTime.Now.Hour + System.DateTime.Now.Minute + System.DateTime.Now.Second + ".png";
string myDefaultLocation = Application.persistentDataPath + "/" + myFileName;
string myFolderLocation = "/storage/emulated/0/DCIM/Camera/JCB/"; //EXAMPLE OF DIRECTLY ACCESSING A CUSTOM FOLDER OF THE GALLERY
string myScreenshotLocation = myFolderLocation + myFileName;
//ENSURE THAT FOLDER LOCATION EXISTS
if (!System.IO.Directory.Exists(myFolderLocation))
{
System.IO.Directory.CreateDirectory(myFolderLocation);
}
ScreenCapture.CaptureScreenshot(myFileName);
//MOVE THE SCREENSHOT WHERE WE WANT IT TO BE STORED
yield return new WaitForSeconds(1);
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
This is the complete code by which we can take snapshots and save to Android Gallery. Let's not try the random Plugins ;D Cheers
Taking a screenshot and moving it works if waitforseconds is greater than 2. And the refreshing the android gallery doesn't work. It just gives a big error
Hi @ShraddhaP @Ginxx009 Thank you for this, I really didn't want to use a plugin so it's great to see a script that deals with it easily. I've copied this script and when I run it, it saves the initial image but doesn't move it to the DCI$$anonymous$$/Camera folder. I get an error saying:
UnauthorizedAccessException: Access to the path "/storage/emulated/0/DCI$$anonymous$$/Camera/JCB/" is denied.
In my Android $$anonymous$$anifest I have the permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
Any ideas what's going wrong here? Thanks
Answer by Ginxx009 · Dec 04, 2017 at 07:23 AM
The documentation of 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 this post , androids have problems encoding to PNG. This postprovided 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 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. This post about forcing a media scan, but I have yet to try out the answer
I have tried this using easy mobile plugin, the screenshots do not appear in the mobile's gallery.
did you try this one
using UnityEngine;
using System.Collections;
public class showscreenshot : $$anonymous$$onoBehaviour {
void On$$anonymous$$ouseDown() {
Application.CaptureScreenshot("Screenshot.png");
}
}
lets try it as a code . If it works tell me so we can proceed to the next step . Lets not use a Plugin . hahaha
Done Lol ;p Now...? Can we move to internal or personal thread?