package com.xuefei.game;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.unity3d.player.UnityPlayerNativeActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
 
public class MainActivity extends UnityPlayerNativeActivity {
    public static Context context;
    public static MainActivity mainActivity;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
 
        super.onCreate(savedInstanceState);
        mainActivity = this;
    }
 
    // 保存到相册
    public static void savePng(final String fileName) {
 
        context = mainActivity.getApplicationContext();
 
        mainActivity.runOnUiThread(new Runnable() {
            public void run() {
 
                Bitmap bitmap = BitmapFactory.decodeFile(Environment
                        .getExternalStorageDirectory()
                        + "/Android/data/com.xuefei.game/files/"
                        + fileName
                        + ".png");
 
                File file = new File(Environment.getExternalStorageDirectory()
                        + "/DCIM/Camera", fileName + ".jpg");
 
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(file);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    Log.w("cat", e.toString());
                }
                bitmap.compress(CompressFormat.JPEG, 100, fos);
 
                try {
                    fos.flush();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    Log.w("cat", e.toString());
                }
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    Log.w("cat", e.toString());
                }
                bitmap.recycle();//扫描保存的图片
                context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" +Environment.getExternalStorageDirectory()
                        + "/DCIM/Camera/"+fileName + ".jpg")));
                 
                Toast.makeText(context, "照片已保存到相册", Toast.LENGTH_SHORT).show();
  
            }
        });
    }
}

Unity中代码:

        if (GUI.Button(new Rect(0, 720, 400, 120), "SavePng"))
        {
            StartCoroutine(SavePngToSD(DateTime.Now.ToFileTime().ToString()));
        }
 
    /// <summary>
    /// 截图后先检测图片是否保存成功然后调用保存到相册
    /// </summary>
    /// <param name="name"></param>
    public void SavePng(string name)
    {
#if UNITY_ANDROID
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
        jo.CallStatic("SavePng", name);
#elif  UNITY_IPHONE
 
#endif
    }
 
/// <summary>
    /// 保存截图到相册
    /// </summary>
    /// <param name="pngName"></param>
    /// <returns></returns>
    IEnumerator SavePngToSD(string pngName)
    {
        yield return new WaitForEndOfFrame();
        Application.CaptureScreenshot(pngName + ".png");
        while (!IsFileExistByPath(Application.persistentDataPath + "/" + pngName + ".png"))
        {
            yield return new WaitForSeconds(0.05f);
        }
        SavePng(pngName);
        yield return new WaitForSeconds(0.3f);
        //测试请求文件
        WWW www = new WWW("file:///" + Application.persistentDataPath + "/" + pngName + ".png");
 
        yield return www;
    }
 
    /// <summary>
    /// 检测文件是否存在
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static bool IsFileExistByPath(string path)
    {
        FileInfo info = new FileInfo(path);
        bool b = false;
        if (info == null || info.Exists == false)
        {
            b = false;
        }
        else
        {
            b = true;
        }
        return b;
    }

注意文件路径是否存在,不存在就创建

流程:Unity截屏--调用安卓拷贝到相册--扫描保存的文件

解决的问题:截屏图片在相册不显示。

1.png
2.png

标签: Unity, Android, 截屏, 相册

添加新评论