-
[Android] 파일 공유 대응 방법(File Provider)Android 2020. 11. 13. 10:46반응형
Android 7.0 이상 버전부터는 파일 공유시에 정책이 변경되었습니다.
그래서 제대로 코드를 짰지만 파일 공유가 안되는 일을 겪으실 수 있습니다.
파일 경로를 file:// 을 사용하면 Fileuriexposedexception가 발생합니다.
안드로이드에서는 파일 공유시 접근 권한을 반드시 줘야하고 content://로
시작 해야합니다. 그리고 FileProvider를 사용하기를 권장하고 있습니다.
FileProvider는 xml에 사용하는 경로별로 정의를 해 줘야 합니다.1. res/xml/provider.xml
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <cache-path name="cache" path="." /> <files-path name="files" path="." /> <external-path name="external" path="."/> <external-cache-path name="external-cache" path="."/> <external-files-path name="external-files" path="."/> </paths>
2. Manifest
<provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}" // 패키지 명 android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider" /> </provider>
이 두 가지를 설정해주면 FileProvider를 사용하여 파일을 공유 할 수 있습니다.
반응형'Android' 카테고리의 다른 글
[Android] TextView 부분 글자 바꾸기 (Span) (0) 2020.12.14 Retrofit2 + RxJava 에서 Header 가져오기 (0) 2020.12.06 [Android] 카카오 로그인 v2 API with kotlin - 1 (0) 2020.10.16 Fragment 통신 (setTargetFragment, Listener) (0) 2020.10.07 BindingAdapter에서 margin 설정하기 (0) 2020.09.24