保存并查看Android设备的快照图像 [英] save and view snapshot image from android device
问题描述
我的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中,我将我的项目作为一个jar文件从
$
$ b
< 项目设置 - >设置为库
,从 libs
中获取 jar文件
,并将其复制到: code>插件/ Android 在我的unity项目目录,然后创建一个新的Manifest.xml如下:
code>< manifest xmlns:android =http://schemas.android.com/apk/res/androidpackage =com.test.app>
< application android:icon =@ drawable / app_iconandroid:label =@ string / app_name>
< activity android:name =。MainActivity
android:label =@ string / app_name
android:configChanges =fontScale | keyboard | keyboardHidden | locale | mnc | mcc |导航|取向|屏幕布置|屏幕尺寸| smallestScreenSize | uiMode |触摸屏>
< 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;
公开GameObject panl;
void Start()
{
AndroidJNI.AttachCurrentThread();
pluginTutorialActivityJavaClass = new AndroidJavaClass(com.test.app.MainActivity);
}
void OnMouseDown()
{
StartCoroutine(takeShot());
}
私有IEnumerator takeShot()
{
panl.SetActive(false); // panl包含具有此脚本的按钮`ScreenShot class`
Application.CaptureScreenshot(my_img.png);
//在移动上它是持久性数据路径
string dir = Application.persistentDataPath +/my_img.png;
float timeOut = 0f;
//检查文件是否存在
while(!File.Exists(dir))
{
Debug.Log(Cant find screenshot!);
timeOut + = Time.deltaTime;
//它需要太多放弃
if(timeOut> 5f)yield break;
yield返回null;
}
//如果您的路径正确
string yourPath = pluginTutorialActivityJavaClass.CallStatic< string>(GetPath);
Debug.Log(yourPath);
File.Copy(dir,yourPath +/my_img.png\",true);
//也许删除源文件在这里?
}
}
但没有任何事情发生,我无法在设备上看到我的屏幕截图
有人可以告诉我我做错了什么?
截图截图后,您需要复制它,在这种情况下,这样的协同工作应该会很好,
private IEnumerator takeShot()
{
Application.CaptureScreenshot(my_img.png);
//在移动上它是持久性数据路径
string dir = Application.persistentDataPath +/my_img.png;
float timeOut = 0f;
//检查文件是否存在
while(!File.Exists(dir))
{
Debug.Log(Cant find screenshot!);
timeOut + = Time.deltaTime;
//它需要太多放弃
if(timeOut> 5f)yield break;
yield返回null;
}
//如果您的路径正确
string yourPath = pluginTutorialActivityJavaClass.CallStatic< string>(GetPath);
Debug.Log(yourPath);
File.Copy(dir,yourPath +/my_img.png\",true);
//也许删除源文件在这里?
}
这是基本的想法,我假设你的java代码获取外部存储路径不要忘记导入 System.IO
。
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 ???
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
.
这篇关于保存并查看Android设备的快照图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!