AOS - Java

[안드로이드/Android] FileUriExposedException :  file:///storage/~~ exposed beyond app through ClipData.Item.getUri()

야라라라 2018. 6. 28. 10:06
반응형
SMALL

안드로이드 7.0 부터의 api 정책 변경

-앱사이의 공유가 더 엄격해져서 file:// URI 가 직접 노출되지 않도록 content:// URI를 보내고 

이에 대해서 임시 액세스 권한을 부여하는 방식으로 변경

(참고)https://developer.android.com/about/versions/nougat/android-7.0-changes.html#accessibility



에러 구문

android.os.FileUriExposedException :  file:///storage/~~.jpg exposed beyond app through ClipData.Item.getUri()



AndroidManifest.xml 수정

<application>
...

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.test.android.test.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">

        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepaths" />

    </provider>

...

</application>




Res/xml/filepaths.xml 생성

<?xml version="1.0" encoding="utf-8"?>

<paths xmlns:android="http://schemas.android.com/apk/res/android">

    <external-path name="storage/emulated" path="."/>

</paths>




코드 대체

Uri uri = Uri.fromFile(mPhotoFile);


-> Uri uri = FileProvider.getUriForFile(getContext(), "com.test.android.test.fileprovider", mPhotoFile);



*빨간 글씨 부분은 개인 도메인으로 AndroidManifest.xml provider의 authorities와 동일해야함

반응형
LIST