unity調用 安卓相冊


 
         

把 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); }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM