본문 바로가기
안드로이드

android.os.FileUriExposedException: file:///storage/emulated/0/... exposed beyond app through Intent.getData()

by Dokon Jang 2020. 2. 1.
반응형

 

안드로이드 프로젝트의 targetSdkVersion이 24이상에서 아래의 소스코드(MIME Type에 따른 앱 연결 화면 오픈)를 실행하면 아래와 같이 오류가 발생합니다.

[소스코드]

String mergeFilePath = "file:///storage/emulated/0/sql.pdf";
File file = new File(mergeFilePath);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Intent intent = Intent.createChooser(target, "Open File");
try {
    startActivity(intent);
} catch (Exception e) {
    Log.e("Error", Log.getStackTraceString(e));
    Toast.makeText(MergeAct.this, "PDF reader doesn't not exist.", Toast.LENGTH_LONG).show();
}

[오류]

android.os.FileUriExposedException: file:///storage/emulated/0/... exposed beyond app through Intent.getData()

 

해결 방법은 FileProvide를 사용하는 방법이 있는데, AndroidManifest.xml 등을 수정해야 하는 등 여러 작업이 필요합니다.

저는 URI exposure를 무시하는 아래의 코드를 onCreate에서 추가하여 해결했습니다.

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());

 

참고 : https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed/55138155

반응형

댓글