Android:Context
어플리케이션 환경에 관한 글로벌 정보를 접근하기 위한 인터페이스. Abstract 클래스이며 실재 구현은 안드로이드 시스템에 의해 제공된다. Context 를 통해, 어플리케이션에 특화된 리소스나 클래스에 접근할 수 있을 뿐만 아니라, 추가적으로, 어플리케이션 레벨의 작업 - Activity 실행, Intent 브로드캐스팅, Intent 수신 등, 을 수행하기 위한 API 를 호출 할 수도 있다. 즉, Context 는 크게 두 가지 역할을 수행하는 Abstract 클래스 입니다.
- 어플리케이션에 관하여 시스템이 관리하고 있는 정보에 접근하기
- 안드로이드 시스템 서비스에서 제공하는 API 를 호출 할 수 있는 기능
getContext() vs getApplicationContext() vs getBaseContext()
- lusalome Blog: getContext(), getApplicationContext(), getBaseContext(), this 차이점
- 愚工移山 (우공이산) Blog: getContext(), getApplicationContext(), getBaseContext(), this 차이점
- View.getContext()
- 현재 실행되고 있는 View의 context를 return 하는데 보통은 현재 활성화된 activity의 context가 된다.
- Activity.getApplicationContext()
- 어플리케이션의 Context가 return된다. 현재 activiy의 context 뿐만 아니라 application의 lifeCycle에 해당하는 Context가 사용된다.
- ContextWrapper.getBaseContext()
- 자신의 Context가 아닌 다른 Context를 access하려 할 때 사용한다. ContextWrapper는 getBaseContext()를 경유해서 Context를 참조할 수 있다.
- this
- View.getContext()와 같다.
미디어 컨텐츠 접근 방법
첫 번째 방법은 MediaColumns.Data
를 사용한다.
Cursor c = getContentResolver().query(Uri.parse"content://media/external/images/media/1"), null,null,null,null);
c.moveToNext();
String path = c.getString(c.getColumnIndex(MediaStore.MediaColumns.DATA));
Uri uri = Uri.fromFile(new File(path));
Log.e("URI", uri.toString());
c.close();
두 번째 방법은 컨텐츠 ID를 사용한다.
String fileStr = "file:///sdcard/DCIM/Camera/2010-03-08%2013.42.44.jpg";
Uri fileUri = Uri.parse(fileStr);
String filePath = fileUri.getPath();
Cursor c = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,null, "_data = '" + filePath + "'", null,null);
c.moveToNext();
int id = c.getInt(0);
Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
Log.e("URI", uri.toString());
Favorite site
References
-
Android_context.pdf ↩