【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" />
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" />
相关文章推荐
- Android计算器界面 TableLayout
- Android几种常见的多渠道(批量)打包方式介绍
- Android短信页面
- android第一步---环境搭建
- Android 中 WebView 与 js 简单交互实现图文混排效果,解决图片自适应屏幕与查看大图问题
- Android--帧动画
- Android下的图片压缩及图片和视频的上传
- Android 自定义控件onTouch事件浅析和个人总结
- Android蓝牙开发SPP协议通信
- Zepto的SwipeUp 在 android 和微信 的解决方案
- Android应用中返回键的监听及处理(含有按两次返回)
- Android知识体系结构
- Android基础进阶之Broadcast(广播接受者)的学习和研究
- android 多媒体数据库(非原创)
- Android 计算器
- Android入门篇(一)Android工程的搭建,导入与导出,图标的修改
- android-引导页的实现方式
- Android电量测试工具battery-historian学习小记
- Android控件之SurfaceView初步了解
- android的Environment类 .