unity android 存入相机相册,Unity调用Android 相机和相册

安卓代码: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:

package="com.xx.xxx"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="15"

android:targetSdkVersion="23" />

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

>

android:name="com.xx.xxx.MainActivity"

android:label="@string/app_name" >

Unity 代码:AndroidPhoto.cs

using UnityEngine;

using System.Collections;

public class AndroidPhoto : MonoBehaviour {

// Use this for initialization

void Start () {

}

//打开相册

public void OpenPhoto()

{

AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");

AndroidJavaObject jo = jc.GetStatic("currentActivity");

jo.Call("OpenGallery");

}

//打开相机

public void OpenCamera()

{

AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");

AndroidJavaObject jo = jc.GetStatic("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().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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值