Android > ±âº» Gallery¿¡¼ ¼±ÅÃµÈ Image °¡Á®¿À±â
µî·ÏÀÏ : 2017-07-06 17:50
Á¶È¸¼ö : 145,036
±âº» Gallery¸¦ ¿¾î Image¸¦ ¼±ÅÃÇØ ¿Ã ¹öÆ° Çϳª¿Í ¼±ÅÃÇÑ Image¸¦ º¸¿©ÁÙ ImageView¸¦ ¹èÄ¡
¹öÆ° Ŭ¸¯ ½Ã ±âº» Gallery·Î À̵¿
public class gallery1 extends Activity {
final int REQ_CODE_SELECT_IMAGE=100;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......
//¹öÆ° Ŭ¸¯½Ã 󸮷ÎÁ÷
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(android.provider.MediaStore.Images.Media.CONTENT_TYPE);
intent.setData(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQ_CODE_SELECT_IMAGE);
......
}
}
¼±ÅÃÇÑ À̹ÌÁö µ¥ÀÌÅÍ ¹Þ±â
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(getBaseContext(), "resultCode : "+resultCode,Toast.LENGTH_SHORT).show();
if(requestCode == REQ_CODE_SELECT_IMAGE)
{
if(resultCode==Activity.RESULT_OK)
{
try {
//Uri¿¡¼ À̹ÌÁö À̸§À» ¾ò¾î¿Â´Ù.
//String name_Str = getImageNameToUri(data.getData());
//À̹ÌÁö µ¥ÀÌÅ͸¦ ºñÆ®¸ÊÀ¸·Î ¹Þ¾Æ¿Â´Ù.
Bitmap image_bitmap = Images.Media.getBitmap(getContentResolver(), data.getData());
ImageView image = (ImageView)findViewById(R.id.imageView1);
//¹èÄ¡ÇسõÀº ImageView¿¡ set
image.setImageBitmap(image_bitmap);
//Toast.makeText(getBaseContext(), "name_Str : "+name_Str , Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
}
}
}
}
Uri ¿¡¼ ÆÄÀϸíÀ» ÃßÃâÇÏ´Â ·ÎÁ÷
public String getImageNameToUri(Uri data)
{
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(data, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String imgPath = cursor.getString(column_index);
String imgName = imgPath.substring(imgPath.lastIndexOf("/")+1);
return imgName;
}
À̹ÌÁö Crop ±â´É ³Ö±â
// cropµÈ À̹ÌÁö°¡ ÀúÀåµÉ °æ·Î¸¦ ¼³Á¤
File tempFile = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");
Uri tempUri = Uri.fromFile(tempFile);
intent.putExtra("crop", "true");
intent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
intent.setType("image/*");
Á¢±Ù ±ÇÇÑÀº ¿ÜÀå¸Þ¸ð¸®¸¦ ¼³Á¤ÇØ Áà¾ß ÇÕ´Ï´Ù.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>