这里说三种方式:
//1. 相册单张照片选择 (Android系统默认提供了该方式)
protected void getImageFromAlbum() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");//相片类型
startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);
}
//2.拍照(Android系统默认提供了该方式)
protected void getImageFromCamera() {
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(getImageByCamera, REQUEST_CODE_CAPTURE_CAMEIA);
}else {
Toast.makeText(getApplicationContext(), "请确认已经插入SD卡", Toast.LENGTH_LONG).show();
}
}
3.从相册选多张图片 需要自己查询来实现
ListmDatas = new ArrayList();
// 只查询jpeg和png的图片
Uri ImagesUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;//获取照片地址uri
ContentResolver contentResolver = mContext.getContentResolver();//创建内容解析者
Cursor cursor = contentResolver.query(ImagesUri, null //数据库查询
, MediaStore.Images.Media.MIME_TYPE + "=? or " + MediaStore.Images.Media.MIME_TYPE + "=?"
, new String[]{"image/jpeg", "image/png"}
, MediaStore.Images.Media.DATE_MODIFIED);
while (cursor.moveToNext()){
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
mDatas.add(path);
}
cursor.close();
最好在onActivityResult()方法里得到
// if(data != null){
// if (requestCode == REQUEST_CODE_PICK_IMAGE) {//相册
// Uri uri = data.getData();
// Log.i("TAG", "uri:--------"+uri);
// byte[] bytes = getBitmapFromUri(uri);
// Glide.with(mContext).load(bytes).into(ivRou);
//
// } else if (requestCode == REQUEST_CODE_CAPTURE_CAMEIA ) {//相机
//
// Uri uri = data.getData();
// byte[] bytes = getBitmapFromUri(uri);
// if(uri == null){
// Bundle bundle = data.getExtras();
// if(bundle != null){
// bytes = (byte[]) bundle.get("data");
// }
// }
// byte[] Bytes = getBitmapFromUri(uri);
// Glide.with(mContext).load(Bytes).into(ivRou);
// }
// }