先說用法html
一、開始選圖(相機、相冊)java
//這句代碼啓動相機拍照 SelectPicUtil.getByCamera(this); //這句代碼跳轉到相冊選圖 SelectPicUtil.getByAlbum(this);
二、處理圖片,在onActivityResult中處理android
protected void onActivityResult(int requestCode, int resultCode, Intent data) { //輸出圖片800*400大小,選擇圖片時的裁剪比例是2:1 bm = SelectPicUtil.onActivityResult(this, requestCode, resultCode, data, 800, 400, 2, 1); if (bm!=null) { ... } }
OK,用法就這麼簡單!目前我測試的全部機型都沒問題(固然不是全部機型),圖片也不用去考慮大小,反正都是存SD卡,不會內存溢出的。不過注意須要讀寫SD卡的權限(通常項目確定是會有的)。app
接下來就是代碼啦ide
←。← copy的時候留個author能夠麼,畢竟人家寫的這麼辛苦。
工具
import java.io.FileNotFoundException; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Environment; import android.provider.MediaStore; /** * 選擇本地圖片工具類 * <br> * 由於直接獲取圖片容易崩潰,因此直接存入SD卡,再獲取 * <br> * 又由於寫法不正確容易致使部分機型沒法使用,因此封裝起來複用 * <br> * 使用方法: * <br> * 一、調用getByAlbum、getByCamera去獲取圖片 * <br> * 二、在onActivityResult中調用本工具類的onActivityResult方法進行處理 * <br> * 三、onActivityResult返回的Bitmap記得空指針判斷 * * <br><br> * PS:本工具類只能處理裁剪圖片,若是不想裁剪,不使用本工具類的onActivityResult,本身作處理便可 * * @author linin630 * */ public class SelectPicUtil { /**臨時存放圖片的地址,如需修改,請記得建立該路徑下的文件夾*/ private static final String lsimg = "file:///sdcard/temp.jpg"; public static final int GET_BY_ALBUM = 801;//若是有衝突,記得修改 public static final int GET_BY_CAMERA = 802;//若是有衝突,記得修改 public static final int CROP = 803;//若是有衝突,記得修改 /**從相冊獲取圖片*/ public static void getByAlbum(Activity act){ Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT); getAlbum.setType("image/*"); act.startActivityForResult(getAlbum, GET_BY_ALBUM); } /**經過拍照獲取圖片*/ public static void getByCamera(Activity act){ String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { Intent getImageByCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); getImageByCamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.parse(lsimg)); getImageByCamera.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); act.startActivityForResult(getImageByCamera, GET_BY_CAMERA); } else { L.e("請確認已經插入SD卡"); } } /** * 處理獲取的圖片,注意判斷空指針,默認大小480*480,比例1:1 */ public static Bitmap onActivityResult(Activity act, int requestCode, int resultCode, Intent data){ return onActivityResult(act, requestCode, resultCode, data, 0, 0, 0, 0); } /** * 處理獲取的圖片,注意判斷空指針 */ public static Bitmap onActivityResult(Activity act, int requestCode, int resultCode, Intent data, int w, int h,int aspectX,int aspectY){ Bitmap bm = null; if (resultCode == Activity.RESULT_OK) { Uri uri = null; switch (requestCode) { case GET_BY_ALBUM: uri = data.getData(); uri = dealUri(act, uri);//適配4.4系統 act.startActivityForResult(crop(uri,w,h,aspectX,aspectY),CROP); break; case GET_BY_CAMERA: uri = Uri.parse(lsimg); act.startActivityForResult(crop(uri,w,h,aspectX,aspectY),CROP); break; case CROP: bm = dealCrop(act); break; } } return bm; } /**默認裁剪輸出480*480,比例1:1*/ public static Intent crop(Uri uri){ return crop(uri,480,480,1,1); } /** * 裁剪,例如:輸出100*100大小的圖片,寬高比例是1:1 * @param w 輸出寬 * @param h 輸出高 * @param aspectX 寬比例 * @param aspectY 高比例 */ public static Intent crop(Uri uri,int w,int h,int aspectX,int aspectY){ if (w==0&&h==0) { w=h=480; } if (aspectX==0&&aspectY==0) { aspectX=aspectY=1; } Intent intent = new Intent("com.android.camera.action.CROP"); // 照片URL地址 intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", aspectX); intent.putExtra("aspectY", aspectY); intent.putExtra("outputX", w); intent.putExtra("outputY", h); // 輸出路徑 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.parse(lsimg)); // 輸出格式 intent.putExtra("outputFormat", "JPEG"); // 不啓用人臉識別 intent.putExtra("noFaceDetection", true); intent.putExtra("return-data", false); return intent; } /**處理裁剪,獲取裁剪後的圖片*/ public static Bitmap dealCrop(Context context){ // 裁剪返回 Uri uri = Uri.parse(lsimg); Bitmap bitmap = null; try { bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri)); } catch (FileNotFoundException e) { e.printStackTrace(); } return bitmap; } /**適配4.4系統*/ public static Uri dealUri(Activity act,Uri uri){ String filePath = null; if(DocumentsContract.isDocumentUri(act, uri)){ String wholeID = DocumentsContract.getDocumentId(uri); String id = wholeID.split(":")[1]; String[] column = { MediaStore.Images.Media.DATA }; String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = act.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[] { id }, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); }else{ String[] projection = { MediaStore.Images.Media.DATA }; Cursor cursor = act.getContentResolver().query(uri, projection, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); filePath = cursor.getString(column_index); } uri = Uri.fromFile(new File(filePath)); return uri; } }
若是的話測試
若是用個人工具類出問題了,麻煩在這裏提一下,我會繼續完善的~~或者有什麼高見也說一下哦~~this
------------------spa
加入適配4.4系統代碼,關鍵代碼在dealUri方法中。.net