본문 바로가기
개발/Android

[Android] exposed beyond app through clipdata.item.geturi 에러 해결

by blacktree 2022. 7. 22.
반응형

앱 화면을 캡처하려고 보니 최신 폰에서 위와 같은 에러가 발생하였다. 

찾아보니 Android 7.0 이상의 경우 앱 외부에 file://URI의 노출을 금지하기 때문에 FileUriExposedException 이 발생한다. 

따라서 파일 공유를 위해서 content://URI 형식을 바꿔주고 권한을 부여해야 한다.

 

해결방법 1 - FileProvider를 생성

1. AndroidManifest.xml에 추가

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="com.example.myproject.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

2. res 파일 -> xml폴더를 생성 -> file_paths.xml 파일 생성

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="image_path" path="" />
</paths>

3. Uri는 FileProvider을 사용

val photoUri = FileProvider.getUriForFile(this, "com.example.myproject.fileprovider", tempFile)

해결방법 2 - ContentResolver에서 찾아서 사용

String path = Environment.getExternalStorageDirectory()+"/DCIM/myproject/" + "20220722103211_capture.jpg";
Cursor cursor = context.getContentResolver().query(contentUri, null, "_data = '" + filePath + "'", null, null);
cursor.moveToNext();
int id = cursor.getInt(cursor.getColumnIndex("_id"));
Uri uri = ContentUris.withAppendedId(contentUri, id);
return uri;

 

728x90
반응형

댓글