//打开相册 private void openAlbum() { Intent selectIntent = new Intent(Intent.ACTION_PICK, null); selectIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); OnActivityForResultUtils.startActivityForResult(this, Constant.IMAGE_PICKER, selectIntent, new SimpleOnActivityForResultCallback() { @Override public void success(Integer resultCode, Intent data) { Uri selectedImage = data.getData(); if (selectedImage != null) { ContentResolver cr = mContext.getContentResolver(); Cursor cursor = cr.query(selectedImage, null, null, null, null); String imagePath = null; if (cursor == null) { imagePath = selectedImage.getPath(); } else { cursor.moveToFirst(); imagePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); } if (FileUtil.existSDCard()) { cropImageFile = new File(Environment.getExternalStorageDirectory(), "/DCIM/camera/"); }else { cropImageFile = Environment.getDataDirectory(); } cropImageFile = FileUtil.createFile(cropImageFile, "IMG_", ".jpg"); //裁剪图片的路径 startPhotoZoom(new File(imagePath),cropImageFile); } } }); } //打开相机 public void openCamera() { Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File takeImageFile; if (FileUtil.existSDCard()) { takeImageFile = new File(Environment.getExternalStorageDirectory(), "/DCIM/camera/"); }else { takeImageFile = Environment.getDataDirectory(); } takeImageFile = FileUtil.createFile(takeImageFile, "IMG_", ".jpg"); //拍照图片的路径 Uri imageUri = null; //判断是否是Android7.0以及更高的版本 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { imageUri = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".fileProvider", takeImageFile); captureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION |Intent.FLAG_GRANT_READ_URI_PERMISSION); } else { imageUri = Uri.fromFile(takeImageFile); } captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); final File finalTakeImageFile = takeImageFile; OnActivityForResultUtils.startActivityForResult(this, Constant.REQUEST_CAPTURE, captureIntent, new SimpleOnActivityForResultCallback() { @Override public void success(Integer resultCode, Intent data) { if (FileUtil.existSDCard()) { cropImageFile = new File(Environment.getExternalStorageDirectory(), "/DCIM/camera/"); }else { cropImageFile = Environment.getDataDirectory(); } cropImageFile = FileUtil.createFile(cropImageFile, "IMG_", ".jpg"); //裁剪图片的路径 startPhotoZoom(finalTakeImageFile,cropImageFile); } }); } /** * 裁剪方法 */ public void startPhotoZoom(File imageFile,File file) { if (!file.getParentFile().exists()){ file.getParentFile().mkdirs(); } Intent intent = new Intent("com.android.camera.action.CROP"); //添加这一句表示对目标应用临时授权该Uri所代表的文件 intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION |Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri outputUri; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){ //7.0 输出uri 后通过FileProvider创建一个content类型的Uri outputUri = Uri.fromFile(file); //这里不能用 下面的不然会出现 裁剪的图片不能保存的提示 // outputUri = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".fileProvider", file); mContext.grantUriPermission(mContext.getPackageName(), outputUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); //7.0 后资源uri获取方式 intent.setDataAndType(getImageContentUri(mContext, imageFile), "image/*"); } else{ intent.setDataAndType(Uri.fromFile(imageFile), "image/*"); outputUri = Uri.fromFile(file); } intent.putExtra("crop", "true");// crop=true 有这句才能出来最后的裁剪页面. intent.putExtra("aspectX", 1);// 这两项为裁剪框的比例.1:1 正方形 intent.putExtra("aspectY", 1); intent.putExtra("outputX", 300);//图片输出大小,可以不需要 intent.putExtra("outputY", 300); // intent.putExtra("circleCrop", true); intent.putExtra("noFaceDetection", true); intent.putExtra("return-data", false); //注意这里的输出的是上面的文件路径的Uri格式,这样在才能获取图片 intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());// 返回格式 OnActivityForResultUtils.startActivityForResult(this, Constant.REQUEST_CODE_CROUP_PHOTO, intent, new SimpleOnActivityForResultCallback() { @Override public void success(Integer resultCode, Intent data) { requestUpHead(cropImageFile); } }); } //7.0 后资源uri获取方式 (没这个会报错) public static Uri getImageContentUri(Context context, File imageFile) { String filePath = imageFile.getAbsolutePath(); Cursor cursor = context.getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Images.Media._ID }, MediaStore.Images.Media.DATA + "=? ", new String[] { filePath }, null); if (cursor != null && cursor.moveToFirst()) { int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID)); Uri baseUri = Uri.parse("content://media/external/images/media"); return Uri.withAppendedPath(baseUri, "" + id); } else { if (imageFile.exists()) { ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.DATA, filePath); return context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); }else { return null; } } }