1. 程式人生 > >android 開啟本地相簿選擇圖片並返回顯示

android 開啟本地相簿選擇圖片並返回顯示

複製程式碼
  1 package com.jerry.crop;
  2 
  3 import java.io.File;
  4 
  5 import android.app.Activity;
  6 import android.content.Intent;
  7 import android.graphics.Bitmap;
  8 import android.net.Uri;
  9 import android.os.Bundle;
 10 import android.os.Environment;
 11 import android.provider.MediaStore;
12 import android.view.View; 13 import android.widget.ImageView; 14 import android.widget.Toast; 15 16 public class MainActivity extends Activity { 17 18 private static final int PHOTO_REQUEST_CAREMA = 1;// 拍照 19 private static final int PHOTO_REQUEST_GALLERY = 2;// 從相簿中選擇 20 private
static final int PHOTO_REQUEST_CUT = 3;// 結果 21 22 private ImageView iv_image; 23 24 /* 頭像名稱 */ 25 private static final String PHOTO_FILE_NAME = "temp_photo.jpg"; 26 private File tempFile; 27 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 super
.onCreate(savedInstanceState); 31 setContentView(R.layout.activity_main); 32 this.iv_image = (ImageView) this.findViewById(R.id.iv_image); 33 } 34 35 /* 36 * 從相簿獲取 37 */ 38 public void gallery(View view) { 39 // 啟用系統圖庫,選擇一張圖片 40 Intent intent = new Intent(Intent.ACTION_PICK); 41 intent.setType("image/*"); 42 // 開啟一個帶有返回值的Activity,請求碼為PHOTO_REQUEST_GALLERY 43 startActivityForResult(intent, PHOTO_REQUEST_GALLERY); 44 } 45 46 /* 47 * 從相機獲取 48 */ 49 public void camera(View view) { 50 // 啟用相機 51 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 52 // 判斷儲存卡是否可以用,可用進行儲存 53 if (hasSdcard()) { 54 tempFile = new File(Environment.getExternalStorageDirectory(), 55 PHOTO_FILE_NAME); 56 // 從檔案中建立uri 57 Uri uri = Uri.fromFile(tempFile); 58 intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); 59 } 60 // 開啟一個帶有返回值的Activity,請求碼為PHOTO_REQUEST_CAREMA 61 startActivityForResult(intent, PHOTO_REQUEST_CAREMA); 62 } 63 64 /* 65 * 剪下圖片 66 */ 67 private void crop(Uri uri) { 68 // 裁剪圖片意圖 69 Intent intent = new Intent("com.android.camera.action.CROP"); 70 intent.setDataAndType(uri, "image/*"); 71 intent.putExtra("crop", "true"); 72 // 裁剪框的比例,1:1 73 intent.putExtra("aspectX", 1); 74 intent.putExtra("aspectY", 1); 75 // 裁剪後輸出圖片的尺寸大小 76 intent.putExtra("outputX", 250); 77 intent.putExtra("outputY", 250); 78 79 intent.putExtra("outputFormat", "JPEG");// 圖片格式 80 intent.putExtra("noFaceDetection", true);// 取消人臉識別 81 intent.putExtra("return-data", true); 82 // 開啟一個帶有返回值的Activity,請求碼為PHOTO_REQUEST_CUT 83 startActivityForResult(intent, PHOTO_REQUEST_CUT); 84 } 85 86 /* 87 * 判斷sdcard是否被掛載 88 */ 89 private boolean hasSdcard() { 90 if (Environment.getExternalStorageState().equals( 91 Environment.MEDIA_MOUNTED)) { 92 return true; 93 } else { 94 return false; 95 } 96 } 97 98 @Override 99 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 100 if (requestCode == PHOTO_REQUEST_GALLERY) { 101 // 從相簿返回的資料 102 if (data != null) { 103 // 得到圖片的全路徑 104 Uri uri = data.getData(); 105 crop(uri); 106 } 107 108 } else if (requestCode == PHOTO_REQUEST_CAREMA) { 109 // 從相機返回的資料 110 if (hasSdcard()) { 111 crop(Uri.fromFile(tempFile)); 112 } else { 113 Toast.makeText(MainActivity.this, "未找到儲存卡,無法儲存照片!", 0).show(); 114 } 115 116 } else if (requestCode == PHOTO_REQUEST_CUT) { 117 // 從剪下圖片返回的資料 118 if (data != null) { 119 Bitmap bitmap = data.getParcelableExtra("data"); 120 this.iv_image.setImageBitmap(bitmap); 121 } 122 try { 123 // 將臨時檔案刪除 124 tempFile.delete(); 125 } catch (Exception e) { 126 e.printStackTrace(); 127 } 128 129 } 130 131 super.onActivityResult(requestCode, resultCode, data); 132 } 133 }
複製程式碼