您的位置:首页 > 移动开发 > Android开发

【Android】图片切割

2016-04-19 22:41 441 查看
调用系统相机或者相册,之后调用系统的剪切,并把剪切后的图片保存到SD卡。

protected void startCamera(DialogInterface dialog) {
dialog.dismiss();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra("camerasensortype", 2);
intent.putExtra("autofocus", true);
intent.putExtra("fullScreen", false);
intent.putExtra("showActionIcons", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
startActivityForResult(intent, PHOTO_CARMERA);
}
protected void startPick(DialogInterface dialog) {
dialog.dismiss();
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
startActivityForResult(intent, PHOTO_PICK);
}
private void startPhotoZoom(Uri uri, int size) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", true);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", size);
intent.putExtra("outputY", size);
intent.putExtra("return-data", true);
startActivityForResult(intent, PHOTO_CUT);
}
private void setPicToView(Intent data) {
Bundle bundle = data.getExtras();
if (null != bundle) {
final Bitmap bmp = bundle.getParcelable("data");
image.setImageBitmap(bmp);

saveCropPic(bmp);
Log.i("MainActivity", tempFile.getAbsolutePath());
}
}
private void saveCropPic(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileOutputStream fis = null;
bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
try {
fis = new FileOutputStream(tempFile);
fis.write(baos.toByteArray());
fis.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != baos) {
baos.close();
}
if (null != fis) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
权限
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: