问题
I'm having a problem with saving image into an android device this is what i did My java class :
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();
}
}
in eclipse i made my project as a jar file from project setting --> set as library
, got the jar file
from libs
and copy past it in : Plugins/Android
in my unity project directory , then created a new Manifest.xml as followed :
<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>
then the unity c# class as followed :
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?
}
}
but nothing is happening , i am not able to see my screenshot on the device can someone tell me what i did wrong ???
回答1:
After taking screenshot you need to copy it, a coroutine like this should work well under this case,
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?
}
This is the basic idea, I assume your java code for getting external storage path works.Also don't forget to import System.IO
.
来源:https://stackoverflow.com/questions/28737976/save-and-view-snapshot-image-from-android-device