由于最近没什么事,把以前经常用到的东西自己重头写一遍,然后记录下来,给需要的朋友使用,也方便自己以后学习
思路:
1.首先选择图片;
2.进行图片裁剪;
3.保存图片到本地;
拍照获取图片代码:
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
startActivityForResult(intent, camera_result_data);
进入相册选取图片代码:
Intent intent;
if (Build.VERSION.SDK_INT < 19) {
intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
} else {
intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
}
创建文件夹代码:
/**
* 创建文件夹 return
*/
public boolean createFile() {
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
Toast.makeText(getApplicationContext(), "请确认已经插入SD卡", Toast.LENGTH_LONG).show();
return false;
} else {
tempFile = new File(Environment.getExternalStorageDirectory().getPath(),
"/demoPath/" + System.currentTimeMillis() + ".png");
if (!tempFile.getParentFile().exists()) {
tempFile.getParentFile().mkdirs();
}
if (!tempFile.exists()) {
try {
tempFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
return true;
}
拍完照片之后进行裁剪:
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(Uri.fromFile(tempFile), "image/*");
图片裁剪代码:
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
intent.putExtra("noFaceDetection", false); // no face detection
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
startActivityForResult(intent, cropImage_result_data);
源码地址:http://download.youkuaiyun.com/detail/bighua_mm/9604732