Skip to content

Instantly share code, notes, and snippets.

@hitapia
Last active December 18, 2015 04:34
Show Gist options
  • Select an option

  • Save hitapia/f4161493d770a78cd3b9 to your computer and use it in GitHub Desktop.

Select an option

Save hitapia/f4161493d770a78cd3b9 to your computer and use it in GitHub Desktop.
Android - Image Crop...
//선택된 이미지를 크롭
private void makeCrop(Uri picUri){
Intent pickImageIntent = new Intent("com.android.camera.action.CROP");
pickImageIntent.setDataAndType(picUri, "image/*");
pickImageIntent.putExtra("crop", "true");
pickImageIntent.putExtra("outputX", 200);
pickImageIntent.putExtra("outputY", 200);
pickImageIntent.putExtra("aspectX", 1);
pickImageIntent.putExtra("aspectY", 1);
pickImageIntent.putExtra("scale", true);
pickImageIntent.putExtra("return-data", true);
startActivityForResult(pickImageIntent, RESULT_CROP_IMAGE);
}
//갤러리에서 이미지 선택
private void getPic(){
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, GET_PIC_CODE);
}
//Bitmap을 파일로 저장
private void storeCropImage(Bitmap bitmap, String filePath) {
File copyFile = new File(filePath);
BufferedOutputStream out = null;
try {
copyFile.createNewFile();
out = new BufferedOutputStream(new FileOutputStream(copyFile));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//Activity Results
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case GET_PIC_CODE:
makeCrop(data.getData());
break;
case RESULT_CROP_IMAGE :
blImgSet = true;
mAppIconPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/temp/"
+ System.currentTimeMillis() + ".jpg";
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap croped = extras.getParcelable("data");
storeCropImage(croped, mAppIconPath);
imgPic.setImageBitmap(croped);
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment