투케이2K

808. (Android/Java) [간단 소스] Intent.createChooser 사용해 파일 공유 수행 시 zip 파일 타입 지정 방법 본문

Android

808. (Android/Java) [간단 소스] Intent.createChooser 사용해 파일 공유 수행 시 zip 파일 타입 지정 방법

투케이2K 2024. 6. 20. 19:43
반응형

[개발 환경 설정]

개발 툴 : AndroidStudio

개발 언어 : Java / Kotlin

 

[소스 코드]

 

========================================================================

            /**
             * -----------------------------------------------------
             * 1. 사전 : AndroidManifest.xml 파일에 provider 지정
             *
             * <provider
             *    android:name="androidx.core.content.FileProvider"
             *    android:authorities="${applicationId}.provider"
             *    android:exported="false"
             *    android:grantUriPermissions="true">
             *    <meta-data
             *           android:name="android.support.FILE_PROVIDER_PATHS"
             *           android:resource="@xml/provider_paths" />
             * </provider>
             * -----------------------------------------------------
             * 2. 사전 : provider.xml 파일에 path 경로 설정
             *
             * <?xml version="1.0" encoding="utf-8"?>
             * <paths xmlns:android="http://schemas.android.com/apk/res/android">
             *     <files-path name="files" path="."/>
             *     <external-files-path name="external_files_path" path="." />
             *     <external-path name="external_files" path="."/>
             * </paths>
             * -----------------------------------------------------
             * */

========================================================================

String filePath = "<특정 앱 저장소 파일 경로 지정 : 내부 저장소>";

File file = new File(filePath);

if (file.exists() == true){ // [파일이 존재하는 경우]

	Uri fileUri = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".provider", file);

	Intent intentShareFile = new Intent(Intent.ACTION_SEND); // [Intent 생성]
	intentShareFile.setType("application/zip"); // TODO [.zip 파일 타입]
	intentShareFile.putExtra(Intent.EXTRA_STREAM, fileUri); // TODO [파일 주소]
	intentShareFile.putExtra(Intent.EXTRA_SUBJECT, "App Log Send"); // TODO [기본 메시지]
	mContext.startActivity(Intent.createChooser(intentShareFile, "Choose")); // TODO [파일 공유]

}

========================================================================

 

반응형
Comments