Why Unity3D Application.CaptureScreenshot for Android doesn't work? - c#

I have tried all this links bellow:
Not getting image from Persistant path in Android using unity3d
https://developer.vuforia.com/forum/unity-3-extension-technical-discussion/capture-screen-problem-androidhelp
http://answers.unity3d.com/questions/731509/applicationcapturescreenshot-doesnt-save-anything.html
I need to set the path for saving the pictures with an application for Android and IOS with UNITY
http://answers.unity3d.com/questions/200173/android-how-to-refresh-the-gallery-.html#answer-893069
Every time i build an apk, install it on Android device, nothing happend.
Log doesn't exists, so i tryed all this links and then go to /DCIM/Camera/ to see if is it saved, no, nothing...
On iOS i did that, and work perfectly.
Here is some code where i stuck... or the things on Android wont work...
I don't know why no error appear or some file strange in folder
void saveImage()
{
#if UNITY_ANDROID
string theFileName = "Screenshot_" + System.DateTime.Now.ToString("MM_dd_yyyy") +"_"+ System.DateTime.Now.ToString("hh_mm_ss") +".png";
// Many different test, trying to discovery witch will work...
Application.CaptureScreenshot( Application.dataPath + "_" + theFileName );
Application.CaptureScreenshot( "../../../../DCIM/Camera/pathroot_" + theFileName );
Application.CaptureScreenshot( "/storage/emulated/0/DCIM/Camera/" + theFileName );
Application.CaptureScreenshot( "file:///storage/emulated/0/DCIM/Camera/" + theFileName );
StartCoroutine(TakeScreenshot()); // Different test
#endif
#if UNITY_IPHONE
// This work... so, does not need to be here...
#endif
}
This is the method that "Coroutine" calls ( copy from one of those links above ):
private IEnumerator TakeScreenshot()
{
yield return new WaitForEndOfFrame();
//INITIAL SETUP
string myFilename = "Screenshot_" + System.DateTime.Now.ToString("MM_dd_yyyy") +"_"+ System.DateTime.Now.ToString("hh_mm_ss") +".png";
string myDefaultLocation = Application.dataPath + "/" + 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/Camera/";
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"
}

Try This: (Tested & Works):
Application.CaptureScreenshot ("screenshot.png");
store screenshot at:
Application.persistentDataPath, "screenshot.png" i.e data/data/package_name/files
void captureScreenshot(string result){
if (result == "true") {
StartCoroutine(CaptureScreen());
}
}
void OnGUI(){
if (GUI.Button (new Rect (20, 70, 100, 45), "Click"))
captureScreenshot ("true");
}
public IEnumerator CaptureScreen()
{
// Wait for screen rendering to complete
yield return new WaitForEndOfFrame();
Application.CaptureScreenshot ("screenshot.png");
string Origin_Path = System.IO.Path.Combine (Application.persistentDataPath, "screenshot.png");
Debug.Log ("Origin_Path save is " + Origin_Path);
// This is the path of my folder.
string Path = "/mnt/sdcard/" + "screenshot.png";
Debug.Log ("Path save is " + Path);
if (System.IO.File.Exists (Origin_Path)) {
System.IO.File.Move (Origin_Path, Path);
Debug.Log ("Path_move save is " + Path);
}
jo.Call<bool> ("screenshotHandler", "screenshot.png");
}
If you want to store in gallery without Move again then you can use:
Application.CaptureScreenshot ("../../../../DCIM/screenshot.png");
If Error: System.IO.File.Exists (Origin_Path)
//returns FALSE even though file exists at Origin_Path.
Please try this:
public IEnumerator CaptureScreen()
{
Application.CaptureScreenshot ("../../../../DCIM/"+fileName);
Debug.Log ("Sanky: Screen Captured");
while (!File.Exists (filePath)) {
Debug.Log ("Sanky: File exist at location"+filePath);
yield return 0;
}
/////// do whatever you want
}

Related

How to save a captured photo from unity in Android Image Gallery?

I have a valid script that takes a photo with the AR camera and saves it in data files of Android, like so :
screenShotName = "Dorot" + System.DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".png";
File.WriteAllBytes(Application.persistentDataPath + "/" + screenShotName, screenshot.EncodeToPNG());
GameObject.Find("MainCanvas").GetComponent<Canvas>().enabled = true;
How can I change the persistentDataPath with the right path of Android Image Gallery.
Thanks.
I am using Unity Native Gallery Plugin to do the same exact thing.
This is my code
public class ScreenshotTaker : MonoBehaviour
{
public bool takingScreenshot = false;
public void CaptureScreenshot()
{
StartCoroutine(TakeScreenshotAndSave());
}
private IEnumerator TakeScreenshotAndSave()
{
takingScreenshot = true;
yield return new WaitForEndOfFrame();
Texture2D ss = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
ss.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
ss.Apply();
// Save the screenshot to Gallery/Photos
string name = string.Format("{0}_Capture{1}_{2}.png", Application.productName, "{0}", System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
Debug.Log("Permission result: " + NativeGallery.SaveImageToGallery(ss, Application.productName + " Captures", name));
takingScreenshot = false;
}
}
You can do it as JackMini36 said in his answer by using the plugin. Or you can write you piece of code do that.
But the challenging part will be to make it cross platform and permission issues on Android devices. Here is nicely describe article where you can learn different way to save screenshot in gallery in unity applications.
http://unitydevelopers.blogspot.com/2018/09/save-screenshot-in-unity3d.html

Android not finding file path Unity 5

So I am using the streaming assets folder to hold some information that I need to run my program. Here is how I am determining the filepath to use:
// Location of the top part of the query
public string fileLocation_queryTop = "/Bro/bro_Headers_TOP.json";
// We are on android
if(Application.platform == RuntimePlatform.Android)
{
assetPath = "jar:file://" + Application.dataPath + "!/assets";
}
// We are on iPhone
else if(Application.platform == RuntimePlatform.IPhonePlayer)
{
assetPath = Application.dataPath + "/Raw";
}
// We are on mac or windows
else
{
assetPath = Application.dataPath + "/StreamingAssets";
}
_query_TOP = File.ReadAllText(assetPath + fileLocation_queryTop);
This works fine on Windows, but I am trying to build for Android today.
Here is error that I am getting
I got the info on what path to use from the Unity docs here
I guess what you are looking for is the Application.streamingAssetsPath. You can find more information about it here.
Since you are on Android, you will have to use the WWW class from Unity to retrieve your files.
EDIT:
Following this link you simply have to adjust the file name and store the result in your _query_TOP variable.
private IEnumerator LoadFile()
{
string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "file.txt");
if(filePath.Contains("://"))
{
WWW www = new WWW(filePath);
yield return www;
if(string.IsNullOrEmpty(www.error))
{
_query_TOP = www.text;
}
}
else
{
_query_TOP = System.IO.File.ReadAllText(filePath);
}
}

UNITY C# Screenshot issue

I'm trying to write some code to take screenshot and save them in android default DCIM path, first part for take screenshot works but second part for move file to Path not work.
public class IS_Screenshot : MonoBehaviour
{
string ScreenShotFile;
void Start ()
{
ScreenShotFile = Application.persistentDataPath + "_Screenshot_" + System.DateTime.Now.ToString("_yyyy-MM-dd_HH-mm-ss") + ".png";
Debug.Log (ScreenShotFile);
}
public void Screen_Shot()
{
try
{
Application.CaptureScreenshot(ScreenShotFile);
string Path = "/mnt/sdcard/DCIM/" + "_Screenshot_" + System.DateTime.Now.ToString("_yyyy-MM-dd_HH-mm-ss_") + ".png";
Debug.Log (Path);
if(System.IO.File.Exists(ScreenShotFile))
{
System.IO.File.Move(ScreenShotFile, Path);
Debug.Log ("Screenshot file saved.");
}
else
{
Debug.Log ("Screenshot file not found.");
}
}
catch(Exception ex)
{
Debug.Log ("Screenshot capture failed. | " + ex);
}
}
}
Use Path.Combine to attach the filename to the persistentDataPath. It may well be that on Android the value of persistentDataPath does not end in a path seperator, which will result in an invalid path.
ScreenShotFile = Path.Combine(Application.persistentDataPath, "_Screenshot_" + System.DateTime.Now.ToString("_yyyy-MM-dd_HH-mm-ss") + ".png");

Loading an assetbundle from the editor

I've created the ability to download and store an asset bundle to my device. Now I'm trying to load an assetbundle from an already downloaded bundle.
I'm saving the bundle like so:
IEnumerator Start ()
{
WWW urlToLoad = new WWW(url);
yield return urlToLoad;
//read in JSON data
string jsonContents = urlToLoad.text;
var n = JSON.Parse(jsonContents);
jsonURL = n["data"][0];
Debug.Log(jsonURL.ToString());
// split up the json and get last element in teh array
string[] splitJSONURL = jsonURL.Split('/');
string bundle = splitJSONURL[splitJSONURL.Length - 1];
Debug.Log("array: " + bundle.ToString());
//StartCoroutine( DownloadAndCache());
// save bundle to the application folder
string path = Application.persistentDataPath + "/" + bundle;
Debug.Log("Path: " + path);
SaveBytesAsFile(path, urlToLoad.bytes);
savedBundleString = Application.persistentDataPath + "/" + bundle;
}
Next I'm trying to load the bundle like this:
IEnumerator LoadLocalBundle(string filePath)
{
print("Loading from local file..." + filePath);
WWW www = new WWW(filePath);
yield return www;
if(www.error == null)
{
print ("loaded local bundle - instantiating :: " + www.assetBundle.mainAsset.name);
GameObject bundleInstance = (GameObject)Instantiate(www.assetBundle.mainAsset);
assetBundle = www.assetBundle;
}
}
void OnGUI()
{
if (GUI.Button(new Rect(10, 70, 50, 30), "Click"))
{
StartCoroutine( LoadLocalBundle(savedBundleString));
}
}
However, when I run this, nothing loads.
What am I doing wrong?

Not getting image from Persistant path in Android using unity3d

I have a script to take screenshot and save that image and to show that image in a plane.
But not getting that texture.
private string path;
void Start () {
path = Application.persistentDataPath;
}
void captureScreeshot(){
Application.CaptureScreenshot(path + "/Screenshot.png");
alert.text = "saved";
StartCoroutine(saveScreenshot());
onetimeBool = false;
}
IEnumerator saveScreenshot(){
WWW imgfile = new WWW("file://" + path+ "/Screenshot.png");
yield return imgfile;
if(imgfile.error==null){
img = new Texture2D(200, 200, TextureFormat.RGB24, false);
imgfile.LoadImageIntoTexture(img);
alert.text = "got image";
GameObject.Find("plane").renderer.material.mainTexture = img;
}else{
print(imgfile.error);
}
}
Images gets saved.
But I cannot open (load) image in system and android.
Getting error in system :
Couldn't open file /Users/ether/AppData/LocalLow/Company/Project/Screenshot.png
How to get it ???
This is because your path is wrong, you should use: application.dataPath, instead of Application.persistentDataPath.

Categories