Unity调用Android 相机和相册
来源:互联网 发布:中世纪2优化9名城作用 编辑:程序博客网 时间:2024/05/12 08:37
安卓代码:MainActivity.java
package com.xx.xxx; //对应unity的包名import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import com.unity3d.player.UnityPlayer;import com.unity3d.player.UnityPlayerActivity;import android.app.Activity;import android.content.ContentResolver;import android.content.Intent;import android.database.Cursor;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.util.Log;public class MainActivity extends UnityPlayerActivity {//public class MainActivity extends Activity {private static final String TAG = MainActivity.class.getSimpleName();private static final int PHOTO_REQUEST_CODE = 1;//相册public static final int PHOTOHRAPH = 2;// 拍照private static final boolean DEBUG = false;//private String unitygameobjectName = "Main Camera";private String unitygameobjectName = "BGPlane"; //Unity 中对应挂脚本对象的名称public static final int NONE = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);// OpenGallery(); } //调用相机 public void takephoto(){ Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "temp.jpg"))); startActivityForResult(intent, PHOTOHRAPH); } //调用相册 public void OpenGallery() { Intent intent = new Intent(Intent.ACTION_PICK,null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*"); startActivityForResult(intent, PHOTO_REQUEST_CODE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (resultCode == NONE){ return; } if(PHOTO_REQUEST_CODE == requestCode){ if(data == null){ return; } Uri uri = data.getData(); String imagePath = getImagePath(uri); if(DEBUG){ Log.d(TAG, imagePath); } //调用unity中方法 GetImagePath(imagePath) UnityPlayer.UnitySendMessage(unitygameobjectName, "GetImagePath", imagePath); } if (requestCode == PHOTOHRAPH) { String path = Environment.getExternalStorageDirectory() + "/temp.jpg"; if(DEBUG){ Log.e("path:", path); } //调用unity中方法 GetTakeImagePath(path) UnityPlayer.UnitySendMessage(unitygameobjectName, "GetTakeImagePath", path); try { Bitmap bitmap = BitmapFactory.decodeFile(path); SaveBitmap(bitmap);} catch (IOException e) {e.printStackTrace();} } } private String getImagePath(Uri uri) { if(null == uri) return null; String path = null; final String scheme = uri.getScheme(); if (null == scheme) { path = uri.getPath(); } else if (ContentResolver.SCHEME_FILE.equals(scheme)) { path = uri.getPath(); } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) { String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = getContentResolver().query(uri, proj, null, null, null); int nPhotoColumn = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); if (null != cursor) { cursor.moveToFirst(); path = cursor.getString(nPhotoColumn); } cursor.close(); }return path; } public void SaveBitmap(Bitmap bitmap) throws IOException { FileOutputStream fOut = null;String path = "/mnt/sdcard/DCIM/"; try { //查看这个路径是否存在, //如果并没有这个路径, //创建这个路径 File destDir = new File(path); if (!destDir.exists()) { destDir.mkdirs(); } String FILE_NAME = System.currentTimeMillis() + ".jpg"; fOut = new FileOutputStream(path + "/" + FILE_NAME) ;} catch (FileNotFoundException e) {e.printStackTrace();}//将Bitmap对象写入本地路径中bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);try {fOut.flush();} catch (IOException e) {e.printStackTrace();}try {fOut.close();} catch (IOException e) {e.printStackTrace();}}}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xx.xxx" android:versionCode="1" android:versionName="1.0" ><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="23" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name="com.xx.xxx.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Unity 代码:AndroidPhoto.cs
using UnityEngine;using System.Collections;public class AndroidPhoto : MonoBehaviour {// Use this for initializationvoid Start () {} //打开相册public void OpenPhoto(){AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");jo.Call("OpenGallery"); } //打开相机public void OpenCamera(){AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");jo.Call("takephoto"); }public void GetImagePath(string imagePath){ if (imagePath == null)return;StartCoroutine("LoadImage",imagePath);}public void GetTakeImagePath(string imagePath){if (imagePath == null)return;StartCoroutine("LoadImage",imagePath);}private IEnumerator LoadImage(string imagePath){WWW www = new WWW ("file://"+imagePath);yield return www;if (www.error == null) { //成功读取图片,写自己的逻辑GetComponent<ChangePhoto>().LoadAndroidImageOK(www.texture);}else{Debug.LogError("LoadImage>>>www.error:"+www.error);}}}
注意:1.android包名要和unity保持一致
2.unitygameobjectName要对应
3.须将unity下的classes.jar拷贝至安卓工程libs下方可正确编译再导出为jar包,unity5.4.1 下classes.jar 地址:C:\Program Files\Unity_5_4_1\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Development\Classes
0 0
- Unity调用Android 相机和相册
- android调用相机和相册
- android调用相机和相册
- Android调用相机和相册
- 关于android webview调用相册和相机
- android 调用相机和获取相册图片
- android 调用系统相机和相册
- Android调用系统相机和相册
- Android调用系统相机和相册-填坑篇
- Android调用系统相机和相册
- Android调用系统相机和相册
- android调用系统相机和相册
- Android调用系统相机和相册-坑篇
- Android调用系统相机和相册
- 关于android webview调用相册和相机
- 基础Android之调用相机和相册
- Android 调用系统相机和相册
- Android调用系统相册和相机拍照
- eclipse中 中文注释乱码
- 净空法师:同修净土与般若
- HTTP文件断点上传
- Proxy动态代理接口
- 决策树分类算法:ID3 & C4.5 & CART
- Unity调用Android 相机和相册
- android:ToolBar详解(手把手教程)
- linux 目录结构
- user版本如何永久性开启adb 的root权限
- 5.1.1-2,XSD简介 beans的XSD文档声明
- Mongodb中对Documents的子集筛选
- 克隆侠 第五代 破解版
- 使用JQ的AJAX来读取JSON文件
- z-stack 各层间通信机制