기술참고자료/Android
[Android] 미디어 갤러리를 이용한 파일첨부
농사꾼봉팔
2011. 10. 18. 10:18
내용은 길게 못 쓰겠다.
어떠한 클릭이나 터치이벤트 발생시, 아래와같은 메서드를 오버라이딩하여 기본 미디어 갤러리로 인텐트를 넘긴다.
@Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN) {
...
//기본 미디어 갤러리에서 선택 후 사용
Intent intent = new Intent();
intent.setAction( Intent.ACTION_GET_CONTENT );
intent.setType( "image/*" );
activity.startActivityForResult( intent, this.requestCode );
} else if (event.getAction() == MotionEvent.ACTION_UP) {
...
}
return false;
}
그리고 보낸 액티비에서는 콜백메서드를 정의해야 한다.
아래 메서드를 이용하여 정의한다.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
// requestCode를 이벤트발생시 1로 정의했기때문에 조건문이 포함됨
if (requestCode == 1) {
//미디어 갤러리를 실행한 뒤의 콜백메서드
try {
if(data!=null){
Uri selPhotoUri = data.getData();
Bitmap selPhoto = Images.Media.getBitmap( getContentResolver(), selPhotoUri );
ImageView iView = (ImageView) findViewById(R.id.resultImage);
iView.setImageBitmap(selPhoto);
TextView filePathTxt = (TextView) findViewById(R.id.filePath);
filePathTxt.setText("");
filePathTxt.setText("경로 : " + selPhotoUri.getPath());
...
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}