从Android设备保存和查看快照图像

时间:2015-02-26 08:52:34

标签: java c# eclipse unity3d unityscript

我遇到了将图像保存到Android设备的问题,这就是我所做的 我的java类:

package com.test.app;

import com.unity3d.player.UnityPlayerActivity;
import android.os.Bundle;

public class MainActivity extends UnityPlayerActivity
{
 protected void onCreate(Bundle myBundle) {
        super.onCreate(myBundle);

    }
public string GetPath(){
  return Environment.getExternalStorageDirectory().toString();
}
}
在eclipse中,我将我的项目作为来自project setting --> set as library的jar文件,从jar file获取libs并将其复制到我的Unity项目目录中的Plugins/Android中,然后创建了一个新的Manifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test.app">
  <application android:icon="@drawable/app_icon" android:label="@string/app_name">
    <activity android:name=".MainActivity"
             android:label="@string/app_name"
             android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
  </application>
</manifest>

然后统一c#类如下:

public class ScreenShot : MonoBehaviour {
  AndroidJavaClass pluginTutorialActivityJavaClass;
  Public GameObject panl;
void Start()
{
    AndroidJNI.AttachCurrentThread();
        pluginTutorialActivityJavaClass = new AndroidJavaClass("com.test.app.MainActivity");
}
void OnMouseDown()
{
  StartCoroutine(takeShot());
}

private IEnumerator takeShot()
 {
   panl.SetActive(false); // the panl contain the button having this script `ScreenShot class` 
   Application.CaptureScreenshot("my_img.png");
   // on mobile it's at persistant data path
   string dir = Application.persistentDataPath + "/my_img.png";
   float timeOut = 0f;
   // check if file exists
   while (!File.Exists(dir))
  {
    Debug.Log("Cant find screenshot!");
    timeOut += Time.deltaTime;
    // it it takes too much give up
    if (timeOut > 5f) yield break; 
    yield return null;           
  }
  // If your path is correct
   string yourPath = pluginTutorialActivityJavaClass.CallStatic<string>("GetPath");
   Debug.Log(yourPath);
  File.Copy(dir, yourPath + "/my_img.png",true);
  // maybe delete source file here?
 }





}

但没有发生任何事情,我无法在设备上看到我的屏幕截图 谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:0)

截屏后你需要复制它,像这样的协程在这种情况下应该可以正常工作,

private IEnumerator takeShot()
{
    Application.CaptureScreenshot("my_img.png");
    // on mobile it's at persistant data path
    string dir = Application.persistentDataPath + "/my_img.png";
    float timeOut = 0f;
    // check if file exists
    while (!File.Exists(dir))
    {
        Debug.Log("Cant find screenshot!");
        timeOut += Time.deltaTime;
        // it it takes too much give up
        if (timeOut > 5f) yield break; 
        yield return null;           
    }
    // If your path is correct
    string yourPath = pluginTutorialActivityJavaClass.CallStatic<string>("GetPath");
    Debug.Log(yourPath);
    File.Copy(dir, yourPath + "/my_img.png",true);
    // maybe delete source file here?
}

这是基本的想法,我假设你的java代码用于获取外部存储路径。也不要忘记导入System.IO