Android呼叫系統, 任意比例裁剪圖片
阿新 • • 發佈:2019-01-03
廢話不多說,直接上程式碼
核心程式碼:
/**
* 跳轉到系統裁剪圖片頁面
* @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);
//顯示圖片的imageview
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()) {
//noinspection ResultOfMethodCallIgnored
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);
}
}