首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么Unity3D Application.CaptureScreenshot for Android不能工作?

为什么Unity3D Application.CaptureScreenshot for Android不能工作?
EN

Stack Overflow用户
提问于 2015-02-24 01:08:40
回答 1查看 4.3K关注 0票数 0

我试过以下所有这些链接:

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上的东西就不能用了..。我不知道为什么文件夹中没有出现错误或者文件奇怪

代码语言:javascript
复制
    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
    }

这是"Coroutine“调用的方法(从上面的一个链接复制):

代码语言:javascript
复制
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"
}
EN

回答 1

Stack Overflow用户

发布于 2016-08-04 16:03:43

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

Application.CaptureScreenshot ("screenshot.png");

存储屏幕截图: Application.persistentDataPath,"screenshot.png“,即数据/数据/包_name/file

代码语言:javascript
复制
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/creshot.png”);

如果错误: System.IO.File.Exists (Origin_Path)

//返回FALSE,即使在Origin_Path中存在文件。

请试试这个:

代码语言:javascript
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28686604

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档