Java 从android设备保存和查看快照图像
我在将图像保存到android设备时遇到问题这就是我所做的 我的java类:Java 从android设备保存和查看快照图像,java,c#,eclipse,unity3d,Java,C#,Eclipse,Unity3d,我在将图像保存到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.
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中,我将我的项目作为jar文件从project setting-->设置为library
,从libs
获取jar文件,并将其复制到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>
然后是unity 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?
}
}
公共类截图:MonoBehavior{
AndroidJavaClass插件IntuitorialActivityJavaClass;
公共游戏对象面板;
void Start()
{
AndroidJNI.AttachCurrentThread();
PlugIntuToriaActivityJavaClass=新的AndroidJavaClass(“com.test.app.MainActivity”);
}
void OnMouseDown()
{
开始例行程序(takeShot());
}
私人IEnumerator takeShot()
{
panl.SetActive(false);//该窗格包含具有以下脚本“ScreenShot class”的按钮
Application.capture截图(“my_img.png”);
//在移动设备上,它位于持久数据路径
string dir=Application.persistentDataPath+“/my_img.png”;
浮动超时=0f;
//检查文件是否存在
而(!File.Exists(dir))
{
Log(“找不到屏幕截图!”);
timeOut+=Time.deltaTime;
//放弃太多了
如果(超时时间>5f)产量中断;
收益返回空;
}
//如果你的路径是正确的
字符串yourPath=pluginTutorialActivityJavaClass.CallStatic(“GetPath”);
Log(yourPath);
Copy(dir,yourPath+“/my_img.png”,true);
//也许在这里删除源文件?
}
}
但是什么也没发生,我无法在设备上看到我的屏幕截图
有人能告诉我我做错了什么吗?拍摄完截图后,你需要复制它,在这种情况下,像这样的协同程序应该可以很好地工作
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?
}
private IEnumerator takeShot()
{
Application.capture截图(“my_img.png”);
//在移动设备上,它位于持久数据路径
string dir=Application.persistentDataPath+“/my_img.png”;
浮动超时=0f;
//检查文件是否存在
而(!File.Exists(dir))
{
Log(“找不到屏幕截图!”);
timeOut+=Time.deltaTime;
//放弃太多了
如果(超时时间>5f)产量中断;
收益返回空;
}
//如果你的路径是正确的
字符串yourPath=pluginTutorialActivityJavaClass.CallStatic(“GetPath”);
Log(yourPath);
Copy(dir,yourPath+“/my_img.png”,true);
//也许在这里删除源文件?
}
这是基本的想法,我假设您的java代码用于获取外部存储路径。也不要忘记导入System.IO
任何日志错误?而Application.CaptureScreenshot
将图像保存到Application.persistentDataPath
,您只需将其复制到SD卡上,不希望出现错误,我怎么能复制它?我照你说的做了,但是我的图像从来没有被复制过我能在应用程序中看到图像。persistentDataPath但是图像没有被复制:/经过一些工作后,我意识到我应该这样称呼它:start例程(takeShot())
这很管用,但是如果我想在Application.capture截图(“my_img.png”)之前隐藏包含按钮的UI.panel代码>副本不起作用,图像仍保留在目录
文件夹中,控制台给我找不到屏幕截图
,我将编辑我的答案以使其清晰。如果您看到dir
文件夹中的图像,它应该会找到我看不到任何问题的图像。也许你应该检查一下你的java代码我真的不知道发生了什么哈哈哈,面板中的按钮是否会对捕获截图造成问题?但是当我按下按钮保存图像时,按钮会显示在截图上,如何在截图中禁用按钮