UnityTestActivity
package com.xys;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
//import android.graphics.Path;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Base64;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;
public class UnityTestActivity extends UnityPlayerActivity {
//public class UnityTestActivity extends Activity {
Context mContext = null;
public static final int NONE = 0;
public static final int PHOTOHRAPH = 1;// 拍照
public static final int PHOTOZOOM = 2; // 缩放
public static final int PHOTORESOULT = 3;// 结果
public static final String IMAGE_UNSPECIFIED = "image/*";
Uri cropImageUri=null;
public final static String FILE_NAME = "image.png";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
}
//Unity中会调用这个方法,用于区分打开摄像机 开始本地相册
public void TakePhoto(String str)
{
if(str.equals("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);
}else
{
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);
startActivityForResult(intent, PHOTOZOOM);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
// 拍照
if (requestCode == PHOTOHRAPH) {
//设置文件保存路径这里放在跟目录下
File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");
startPhotoZoom(Uri.fromFile(picture));
}
if (data == null)
return;
// 读取相册缩放图片
if (requestCode == PHOTOZOOM) {
startPhotoZoom(data.getData());
}
// 处理结果
if (requestCode == PHOTORESOULT) {
if (data != null){
try {
Bitmap photo = BitmapFactory.decodeStream(getContentResolver().openInputStream(cropImageUri));
String str=null;
ByteArrayOutputStream bStream=new ByteArrayOutputStream ();
photo.compress(Bitmap.CompressFormat.PNG, 100, bStream);
byte[]bytes=bStream.toByteArray();
str=Base64.encodeToString(bytes,Base64.DEFAULT);
UnityPlayer.UnitySendMessage("GameObj","HandleAndroidMessage", str);
}catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
//更改前
super.onActivityResult(requestCode, resultCode, data);
}
public void startPhotoZoom(Uri uri) {
File CropPhoto=new File(getExternalCacheDir(),"crop.jpg");
try{
if(CropPhoto.exists()){
CropPhoto.delete();
}
CropPhoto.createNewFile();
}catch(IOException e){
e.printStackTrace();
}
cropImageUri=Uri.fromFile(CropPhoto);
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
if (Build.VERSION.SDK_INT >= 24) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加这一句表示对目标应用临时授权该Uri所代表的文件
}
intent.putExtra("crop", "true");
intent.putExtra("scale", true);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
//输出的宽高
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 300);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, cropImageUri);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true); // no face detection
startActivityForResult(intent, PHOTORESOULT);
}
}
Manifest.XML
package="com.xys"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="21" />
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
android:name=".UnityTestActivity"
android:label="@string/app_name" >
Unity 脚本
public void OnClickLeft(){
text1.text="点击相册";
//调用我们制作的Android插件打开手机相册
AndroidJavaClass jc = new Android;
Android;
jo.Call("TakePhoto","takeSave");
}
void message(string str){
text1.text = text1.text+="收到图片";
Base64ToTexter2d (str);
}
private void Base64ToTexter2d(string Base64STR)
{
Texture2D pic = new Texture2D (200, 200);
byte[] data = System.Convert.FromBase64String (Base64STR);
pic.LoadImage (data);
skin.texture = pic;
}
就这样 不懂得先看下其他大神的文章