废话不多说,直接上代码
核心代码:
/**
* 跳转到系统裁剪图片页面
* @param imagePath 需要裁剪的图片路径
*/
private void cropPic(String imagePath) {
File file = new File(imagePath);
Intent intent = new Intent("com.android.camera.action.CROP");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(this, "com.leon.crop.fileprovider", file);
intent.setDataAndType(contentUri, "image/*");
} else {
intent.setDataAndType(Uri.fromFile(file), "image/*");
}
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0.1);
intent.putExtra("aspectY", 0.1);
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
intent.putExtra("scale", true);
startActivityForResult(intent, CROP_PHOTO);
}
进入到裁剪页面, 可以自由选择裁剪的比例:
代码如下:
public class MainActivity extends AppCompatActivity {
public final int CROP_PHOTO = 10;
public final int ACTION_TAKE_PHOTO = 20;
private ImageView ivImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivImage = (ImageView) findViewById(R.id.ivImage);
findViewById(R.id.btnPhoto).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
takePhoto();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
File mPhotoFile = new File(getAppFile(this, "images/user_take.jpg"));
switch (requestCode) {
case CROP_PHOTO:
if (data != null) {
Bitmap bitmap = data.getExtras().getParcelable("data");
ivImage.setImageBitmap(bitmap);
}
if (mPhotoFile.exists()) {
mPhotoFile.delete();
}
break;
case ACTION_TAKE_PHOTO:
if (mPhotoFile.exists()) {
cropPic(getAppFile(this, "images/user_take.jpg"));
}
break;
}
}
/**
* 拍照
*/
private void takePhoto() {
try {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File file = new File(getAppFile(this, "images"));
File mPhotoFile = new File(getAppFile(this, "images/user_take.jpg"));
if (!file.exists()) {
file.mkdirs();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(this, "com.pandaq.pandaeye.fileprovider", mPhotoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
} else {
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mPhotoFile));
}
startActivityForResult(intent, ACTION_TAKE_PHOTO);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取本应用在系统的存储目录
*/
public static String getAppFile(Context context, String uniqueName) {
String cachePath;
if ((Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|| !Environment.isExternalStorageRemovable())
&& context.getExternalCacheDir() != null) {
cachePath = context.getExternalCacheDir().getParent();
} else {
cachePath = context.getCacheDir().getParent();
}
return cachePath + File.separator + uniqueName;
}
/**
* 跳转到系统裁剪图片页面
* @param imagePath 需要裁剪的图片路径
*/
private void cropPic(String imagePath) {
File file = new File(imagePath);
Intent intent = new Intent("com.android.camera.action.CROP");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(this, "com.leon.crop.fileprovider", file);
intent.setDataAndType(contentUri, "image/*");
} else {
intent.setDataAndType(Uri.fromFile(file), "image/*");
}
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0.1);
intent.putExtra("aspectY", 0.1);
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
intent.putExtra("scale", true);
startActivityForResult(intent, CROP_PHOTO);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)