2021. 6. 3. 15:13ㆍAndroid/Contents
- Content Provider : 앱간의 데이터 공유를 위해 사용(요청받은 URI로 내부데이터를 검색해 다른 앱에 앱 데이터 제공)
- Content Resolver : 다른 앱의 데이터를 사용하기 위해 URI를 통해 Content Provider에게 요청
- Content Provider
1. ContentProvider 클래스(또는 상속받는 클래스) 생성 시, 자동으로 onCreate(), query(), insert(), delete(), update() 함수가 만들어짐(onCreate()에서 데이터베이스를 생성(DBHandler) 하고, query()에서 데이터 조회, insert()에서 데이터 추가, delete()에서 데이터 삭제, update()에서 데이터 수정)
2. URI 구조 - PREFIX(content)://AUTHORITY(패키지명)/PATH/ID
ex) content://com.test.contentprovider/path/id
참고> UriMatcher 사용해 Content Provider가 받는 uri를 결정할 수 있음(if보다 효율적으로 구분할 수 있기 때문에 사용)
https://posnopi13.tistory.com/18
ContentProvider 사용하기 위한 밑밥 1. UriMatcher 란 무엇일까?
http://developer.android.com/reference/android/content/UriMatcher.html#NO_MATCH <안드로이드 디벨러퍼 사이트> UriMatcher란 무엇일까? 라는 질문이 언제 생각날까? 바로 사용할 때이다. 그렇다면 언제 사용..
posnopi13.tistory.com
https://blog.yena.io/studynote/2017/11/11/Android-Content-Provider.html
[Android] Content Provider 컨텐트 프로바이더
Content Provider 앱과 앱 저장소 사이에서 데이터 접근을 쉽게 하도록 관리해주는 클래스. 왜 쓸까? 앱의 직접적인 코드 변경 없이 데이터 접근/변경할 수 있도록 해줌. Loader나 CursorAdapter 같은 클래
blog.yena.io
- File Provider(Content Provider 상속 파일 버전)
1. AndroidManifest에 선언 시, metadata에 path를 xml 파일로 선언할 수 있음
> AndroidManifest.xml
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
> provider_paths.xml
<paths xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:ignore="UnusedResources">
<!--<external-path name="cache" path="." />-->
<external-path name="external_files" path="." />
<!--<files-path path="." name="cache" />-->
</paths>
참고 > provider_paths.xml에서
<file-paths> : Context.getFilesDir()
<cache-paths> : Context.getCacheDir()
<external-files-path> : Context.getExternalFilesDir(String)
<external-cache-path> : Context.getExternalCacheDir()
<external-media-path> : Context.getExternalmediaDirs()
<external-path> : Environment.getExternalStorageDirectory()
name : 실제 path를 숨기기위한 명칭(URI에서 실제 path 대신 사용됨)
path : 실제 path
https://aroundck.tistory.com/7287
[android] FileProvider 에 알아보자
- FileProvider 는 ContentProvider 의 subclass 로 secure 한 file share 를 관장한다. 이를 통하면 file:/// 형태의 uri 대신 content:// 형태의 uri 를 사용하게 된다. - content URI 는 read, write access..
aroundck.tistory.com
참고 코드
public void viewFile(Context ctx, File file) {
Intent fileLinkIntent = new Intent(Intent.ACTION_VIEW);
fileLinkIntent.addCategory(Intent.CATEGORY_DEFAULT);
//확장자 구하기
String fileExtend = getExtension(file.getAbsolutePath());
// 파일 확장자 별로 mime type 지정해 준다.
if (fileExtend.equalsIgnoreCase("pdf")) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
fileLinkIntent.setDataAndType(Uri.fromFile(file), "application/pdf");
} else {
File path = new File(ctx.getExternalFilesDir("directory").getAbsolutePath());
File newFile = new File(path, file.getName());
Uri contentUri = FileProvider.getUriForFile(ctx, mActivity.getApplicationContext().getPackageName() + ".fileprovider", newFile);
fileLinkIntent.setDataAndType(contentUri, "application/pdf");
fileLinkIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
PackageManager pm = ctx.getPackageManager();
List<ResolveInfo> list = pm.queryIntentActivities(fileLinkIntent,
PackageManager.GET_META_DATA);
if (list.isEmpty()) {
if (fileExtend.equalsIgnoreCase("pdf")) {
showOtherWebBrowser(ctx, PDFVIEWERURL);
}
} else {
ctx.startActivity(fileLinkIntent);
}
}
'Android > Contents' 카테고리의 다른 글
Docker를 이용한 CICD 구성 (0) | 2023.10.19 |
---|---|
DeepLink(feat.Navigation Dynamic Link 설정) (0) | 2023.07.13 |
Kotlin Style Guide vs. Android Studio Lint & Coding Style (0) | 2022.04.23 |
Scoped Storage (1) | 2021.06.03 |