码迷,mamicode.com
首页 > 移动开发 > 详细

unity调用 安卓相册

时间:2015-08-26 19:31:36      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:


把 avtivity.java 里面的内容拷贝到你的mainActivity里面


在unity里面调用TakePhoto


SdkUtil.SendMessageOpenPhoto(FILE_NAME);这个我注释掉的地方是回调到unity里面的
访问路径用 Application.persistentDataPath/FILE_NAME 就能拿到图片





public
void TakePhoto(String str,String path) { 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); } } public void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, IMAGE_UNSPECIFIED); intent.putExtra("crop", "true"); // aspectX aspectY ? ?? intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY ?ü ?? intent.putExtra("outputX", 300); intent.putExtra("outputY", 300); intent.putExtra("return-data", true); startActivityForResult(intent, PHOTORESOULT); } public void SaveBitmap(Bitmap bitmap) throws IOException { FileOutputStream fOut = null; //注解1 String path = "/mnt/sdcard/Android/data/com.sainthyler.muffin/files"; try { //查看这个路径是否存在, //如果并没有这个路径, //创建这个路径 File destDir = new File(path); if (!destDir.exists()) { destDir.mkdirs(); } fOut = new FileOutputStream(path + "/" + FILE_NAME) ; } catch (FileNotFoundException e) { e.printStackTrace(); } //将Bitmap对象写入本地路径中,Unity在去相同的路径来读取这个文件 bitmap.compress(Bitmap.CompressFormat.JPEG, 50, fOut); try { fOut.flush(); } catch (IOException e) { e.printStackTrace(); } try { fOut.close(); } catch (IOException e) { e.printStackTrace(); } //发送成功消息到unity里面 //SdkUtil.SendMessageOpenPhoto(FILE_NAME); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != NONE) { // 返回的路径 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) { Bundle extras = data.getExtras(); if (extras != null) { Bitmap photo = extras.getParcelable("data"); try { SaveBitmap(photo); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } super.onActivityResult(requestCode, resultCode, data); }

 

unity调用 安卓相册

标签:

原文地址:http://www.cnblogs.com/bambomtan/p/4761033.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!