Android > ¾Ù¹ü¿¡¼ À̹ÌÁö ¼±ÅÃÈÄ Crop ÈÄ Imageview ¿¡ ¶ç¿ì±â
µî·ÏÀÏ : 2017-07-06 17:53
Á¶È¸¼ö : 53,239
Gallery ȤÀº Album ¿¡¼ºÎÅÍ Image¸¦ Crop Çϸç Pick ÇÏ´Â ¹æ¹ý¿¡ ´ëÇÑ Sample Code ¸¦ Á¦°øÇÑ´Ù.
¼³¸íÀº ÁÖ¼®À» Âü°íÇϱ⠹ٶø´Ï´Ù.
public class WriteActivity extends Activity implements OnClickListener{
private static final int PICK_FROM_ALBUM = 1; //¾Ù¹ü ¼±ÅÃ
private static final String TYPE_IMAGE = "image/*";
private static final int PROFILE_IMAGE_ASPECT_X = 1;
private static final int PROFILE_IMAGE_ASPECT_Y = 1;
private static final int PROFILE_IMAGE_OUTPUT_X = 600;
private static final int PROFILE_IMAGE_OUTPUT_Y = 600;
private static final String TEMP_FILE_NAME = "tempFile.jpg";
private ImageView mPhotoImageView;
private Uri mTempImageUri;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wirte);
Button btn = (Button) findViewById(R.id.btn_send);
btn.setOnClickListener(this);
Button album_btn = (Button) findViewById(R.id.btn_album);
album_btn.setOnClickListener(this);
Button camera_btn = (Button) findViewById(R.id.btn_camera);
camera_btn.setOnClickListener(this);
mPhotoImageView = (ImageView) findViewById(R.id.img_imageview);
}
public void onClick(View v) {
if (v.getId() == R.id.btn_send) {
//Àü¼Û¹öÆ°À» ´·¶À»¶§ ¹ß»ýÇÑ´Ù.
} else if (v.getId() == R.id.btn_album) {
//¾Ù¹ü¹öÆ° ´·¶À»¶§ À̺¥Æ® ¹ß»ý
doAlbumCrop();
} else if (v.getId() == R.id.btn_camera) {
//Ä«¸Þ¶ó ¹öÆ° ´·¶À»¶§ À̺¥Æ® ¹ß»ý
}
}
private File getTempFile(){
File file = new File( Environment.getExternalStorageDirectory(), TEMP_FILE_NAME );
try{
file.createNewFile();
}
catch( Exception e ){
Log.e("cklee", "fileCreation fail" );
}
return file;
}
private Uri getJustTakenPictureUri(){
Cursor cursor = getContentResolver().query( Images.Media.EXTERNAL_CONTENT_URI,
new String[]{ Images.ImageColumns.DATA }, null, null, null );
if ( cursor == null ) return null;
String fileName = null;
if ( cursor.moveToLast())
fileName = cursor.getString(0 );
cursor.close();
if ( TextUtils.isEmpty( fileName ) ) return null;
return Uri.fromFile( new File( fileName ) );
}
//¾Ù¹ü »çÁø Å©·ÓÇÏ´Â ºÎºÐ
private void doAlbumCrop(){
mTempImageUri = Uri.fromFile( getTempFile() );
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType( TYPE_IMAGE );
intent.putExtra( "crop", "true" );
intent.putExtra( "aspectX", PROFILE_IMAGE_ASPECT_X );
intent.putExtra( "aspectY", PROFILE_IMAGE_ASPECT_Y );
intent.putExtra( "outputX", PROFILE_IMAGE_OUTPUT_X );
intent.putExtra( "outputY", PROFILE_IMAGE_OUTPUT_Y);
intent.putExtra( "scale", true );
intent.putExtra( MediaStore.EXTRA_OUTPUT, mTempImageUri );
intent.putExtra( "outputFormat", Bitmap.CompressFormat.JPEG.toString() );
startActivityForResult( intent, PICK_FROM_ALBUM );
}
//À̹ÌÁöºä¿¡ ¼±ÅÃÇÑ ÆÄÀÏÀ» Ãâ·ÂÇÕ´Ï´Ù.
private void doImageView(){
File tempFile = getTempFile();
if ( tempFile.exists() )
((ImageView)findViewById( R.id.img_imageview )).setImageBitmap( BitmapFactory.decodeFile( tempFile.toString()));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_FROM_ALBUM: {
doImageView();
}
}
}
}
Crop ÇÏ¿© ¹ÞÀ» »çÁøÀÇ ¿ë·®ÀÌ ÀûÀº °æ¿ì¿¡´Â return-data ¶ó´Â extra ¸¦ ³Ö¾î¼ bitmap ÇüÅ·Π¹Ù·Î ¹ÞÀ» ¼ö ÀÖ´Ù.
ÇÏÁö¸¸ android bundle ÀÚüÀÇ ¿ë·® Á¦ÇѶ§¹®¿¡ ¿ë·®ÀÌ Å« °æ¿ì¿¡´Â IBinder ¾î¼±¸ Àú¼±¸ ÇÏ¸é¼ ¿¡·¯°¡ ¹ß»ýÇÑ´Ù.
µû¶ó¼ Å« ¿ë·®ÀÇ image¸¦ cropÇÏ´Â °æ¿ì´Â crop À» ÇÏ¸é¼ ±×°ÍÀ» ¹Ù·Î ÆÄÀÏ·Î ÀúÀåÇÏ°í,
±×°ÍÀ» onActivityResult ¿¡¼ ´Ù½Ã ÀоîÁÖ´Â ¹æ½ÄÀ¸·Î ÇØ°áÇÑ´Ù.
Àüü ÇÁ·ÎÁ§Æ® ÆÄÀÏ Ã·ºÎÇÕ´Ï´Ù.
test_join.zip