/** * 裁剪原始的图片 */
public static final int PHOTOZOOM = 2; // 缩放 public static final int PHOTORESOULT = 3;// 结果
Uri mUritempFile;
@PermissionYes(200)
//申请权限成功打开相册
private void
getMultiYes(
@NonNull List<String> grantedPermissions) {
Intent local =
new Intent()
;
local.setType(
"image/*")
;
local.setAction(Intent.
ACTION_GET_CONTENT)
;
startActivityForResult(local
,
2)
;}
public void
cropRawPhoto(Uri uri) { Intent intent =
new Intent(
"com.android.camera.action.CROP")
;
intent.setDataAndType(uri
,
"image/*")
;
// 设置裁剪
intent.putExtra(
"crop"
,
"true")
;
// aspectX , aspectY :宽高的比例
intent.putExtra(
"aspectX"
,
1)
;
intent.putExtra(
"aspectY"
,
1)
;
// outputX , outputY : 裁剪图片宽高
intent.putExtra(
"outputX"
,
100)
;
intent.putExtra(
"outputY"
,
100)
;
String dateFolder =
new SimpleDateFormat(
"yyyyMMdd"
, Locale.
CHINA) .format(
new Date())
;
//设置图片保存路径
String fff =
"file://" +
"/" + Environment.
getExternalStorageDirectory() +
"/DCIM/"
;
//设置图片名称
File fullFile =
new File(fff
, dateFolder +
".jpeg")
;
//将图片路径转换成uri
mUritempFile = Uri.
parse(fullFile.toString())
;
intent.putExtra(MediaStore.
EXTRA_OUTPUT
,
mUritempFile)
;
intent.putExtra(
"outputFormat"
, Bitmap.CompressFormat.
JPEG.toString())
;
intent.putExtra(
"return-data"
, false)
;
startActivityForResult(intent
,
PHOTORESOULT)
;
}
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == PHOTOZOOM&& data != null) {//这是获取的图片保存在sdcard中的位置 cropRawPhoto(getPictureUri(data)); }if (requestCode == PHOTORESOULT && data != null) {Map<String, String> map = new HashMap<>(); map.put("client_type", "3"); map.put("type", "headimg"); map.put("token", ApiHost.getToken()); LogUtil.e("LXY", getPictureUri(mUritempFile).toString()); ImageDown.getFile(this, mUritempFile, map); }super.onActivityResult(requestCode, resultCode, data); }
/** * 解决小米手机上获取图片路径为null的情况 */ public Uri getPictureUri(android.content.Intent intent) {Uri uri = intent.getData(); String type = intent.getType(); if (uri.getScheme().equals("file") && (type.contains("image/"))) {String path = uri.getEncodedPath(); if (path != null) {path = Uri.decode(path); ContentResolver cr = getContentResolver(); StringBuffer buff = new StringBuffer(); buff.append("(").append(MediaStore.Images.ImageColumns.DATA).append("=").append("'" + path + "'").append(")"); Cursor cur = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.ImageColumns._ID}, buff.toString(), null, null); int index = 0; for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {index = cur.getColumnIndex(MediaStore.Images.ImageColumns._ID); // set _id value index = cur.getInt(index); }if (index == 0) {// do nothing } else {Uri uri_temp = Uri.parse("content://media/external/images/media/" + index); if (uri_temp != null) {uri = uri_temp; }}}}return uri; }/** * 解决小米手机上获取图片路径为null的情况 */ public Uri getPictureUri(Uri uri) {if (uri.getScheme().equals("file")) {String path = uri.getEncodedPath(); if (path != null) {path = Uri.decode(path); ContentResolver cr = getContentResolver(); StringBuffer buff = new StringBuffer(); buff.append("(").append(MediaStore.Images.ImageColumns.DATA).append("=").append("'" + path + "'").append(")"); Cursor cur = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.ImageColumns._ID}, buff.toString(), null, null); int index = 0; for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {index = cur.getColumnIndex(MediaStore.Images.ImageColumns._ID); // set _id value index = cur.getInt(index); }if (index == 0) {// do nothing } else {Uri uri_temp = Uri.parse("content://media/external/images/media/" + index); if (uri_temp != null) {uri = uri_temp; }}}}return uri; }