【问题标题】:Why Unity3D Application.CaptureScreenshot for Android doesn't work?为什么 Android 的 Unity3D Application.CaptureScreenshot 不起作用?
【发布时间】:2015-02-24 01:08:40
【问题描述】:

我已经尝试了以下所有这些链接:

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

每次我构建一个 apk,将其安装在 Android 设备上,什么都没有发生。 日志不存在,所以我尝试了所有这些链接,然后去 /DCIM/Camera/ 看看它是否保存,不,什么都没有......

在 iOS 上我做到了,并且完美运行。

这是我卡住的一些代码...或者 Android 上的东西无法正常工作... 我不知道为什么没有出现错误或文件夹中有一些奇怪的文件

    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
    }

这是“协程”调用的方法(从上面的链接之一复制):

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

【问题讨论】:

    标签: c# android unity3d


    【解决方案1】:

    试试这个:(测试和工作):

    Application.CaptureScreenshot("screenshot.png");

    将屏幕截图存储在: Application.persistentDataPath, "screenshot.png" 即 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");
        }
    

    如果你想在没有移动的情况下存储在画廊中,那么你可以使用:

    Application.CaptureScreenshot("../../../../DCIM/screenshot.png");

    如果错误:System.IO.File.Exists (Origin_Path)
    //即使文件存在于 Origin_Path,也返回 FALSE。

    请试试这个:

    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
    }
    

    【讨论】:

    • 您使用的是哪个 Unity?
    • 5.3.4f1 : Application.CaptureScreenshot ("screenshot.png");将屏幕截图存储在 data/data/packagename/files/xyz.png
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多