unity3d 保存(截图、拍照)图片 到Android IPhone手机相册

亲测可行:

http://www.taidous.com/bbs/forum.php?mod=viewthread&tid=24476


总结:

第一步:新建Android工程,导入unity class.jar包(Unity下找classes.jar, 具体路径为:Unity\Editor\Data\PlaybackEngines\androidplayer\release\bin\classes.jar),写一个保存函数:

package com.zz.cutimage;


import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Field;
 
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


import com.unity3d.player.UnityPlayerActivity;


public class MainActivity extends UnityPlayerActivity {
         
//        Button btn ;
//        @Override
//        protected void onCreate(Bundle savedInstanceState) {
//                super.onCreate(savedInstanceState);
//                setContentView(R.layout.main);
//                btn = (Button)findViewById(R.id.btn);
//                btn.setOnClickListener(new OnClickListener() {
//                        
//                        @Override
//                        public void onClick(View arg0) {
//                                
//                                saveImageToGallery(MainActivity.this, "Environment.getExternalStorageDirectory().getPath() + File.separator + taidous/a.jpg");
//                        }
//                });
//        }
         
        public static void SaveImageToGallery(String path) 
        {
            try{
                Class m_ClassUnityPlayer = Class.forName("com.unity3d.player.UnityPlayer");
                Field m_CurrentActivityFieldUnityPlayer = m_ClassUnityPlayer.getField("currentActivity");
                Activity currentActivity = (Activity) m_CurrentActivityFieldUnityPlayer.get(m_ClassUnityPlayer);
                Context context = currentActivity;
                File file = new File(path);
                Log.i("SaveImageToGallery", file.getPath() + "--" + file.length());
                String url = "";
                // 其次把文件插入到系统图库
                try {
                        url = MediaStore.Images.Media.insertImage(context.getContentResolver(),
                                            file.getAbsolutePath(), file.getName(), null);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                Log.i("SaveImageToGallery", file.getPath() + "--" + file.length());
                // 最后通知图库更新
                context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getPath())));
            }catch(Exception e)
            {
                     Log.e("SaveImageToGallery", e.getMessage());
            }
        }
}

第二部:导出jar包到unity,unity在Androidadminfest中包含包:

            



第三部:unity脚本中调用:

public void OnClickSavePhoto()
{
	var bytes = photoTex2d.EncodeToPNG();
	string path = Application.persistentDataPath + "/" + System.DateTime.Now.ToLongTimeString() + ".png";
#if UNITY_EDITOR
	File.WriteAllBytes(Application.dataPath + "/SavedScreen.png", bytes);
#elif UNITY_ANDROID || UNITY_IPHONE
	File.WriteAllBytes(path, bytes);
#endif
	sharePhotoPanel.SetActive(false);


#if UNITY_ANDROID
	try
	{
		AndroidJavaClass testActivityClass = new AndroidJavaClass("com.zz.cutimage.MainActivity");
		Debug.LogWarning("screenShot in android");
		if (testActivityClass != null)
		{
			Debug.LogWarning("testActivityClass note null");
			object[] objs = new object[] { path };
			testActivityClass.CallStatic("SaveImageToGallery", objs);
		}
	}
	catch (System.Exception e)
	{
		Debug.LogError(e);
	}
#endif
}



你可能感兴趣的:(Unity3D)