파일 공유 설정
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
내 앱에서 다른 앱으로 파일을 안전하게 제공하려면
콘텐츠 URI 형식으로 파일에 대한 보안 핸들입니다. Android
FileProvider
구성요소는
. 이 강의에서는
FileProvider
를 앱에 구현하고
다른 앱에 제공할 파일을 지정할 수 있습니다.
참고: FileProvider
클래스는
AndroidX 핵심 라이브러리. 정보
자세히 알아보려면
종속 항목 선언.
FileProvider 지정
앱의 FileProvider
를 정의하려면
매니페스트를 참조하세요 이 항목은 콘텐츠 URI 생성에 사용할 권한을 지정하고
앱이 공유할 수 있는 디렉터리를 지정하는 XML 파일의 이름입니다.
다음 스니펫은 매니페스트에
<provider>
요소가 포함되어 있습니다.
FileProvider
클래스, 권한 및
XML 파일 이름:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<application
...>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.myapp.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
...
</application>
</manifest>
이 예에서 android:authorities
속성은
API가 생성한 콘텐츠 URI에
FileProvider
입니다.
이 예에서 권한은 com.example.myapp.fileprovider
입니다. 개인용
앱의
문자열 'fileprovider'가 포함된 android:package
값 추가됩니다. 자세히 알아보기
자세한 내용은
콘텐츠 URI 및
android:authorities
속성
<meta-data>
하위 요소
<provider>
는 원하는 디렉터리를 지정하는 XML 파일을 가리킵니다.
공유할 수 있습니다. android:resource
속성은
.xml
확장자로 대체되었습니다.이 파일의 콘텐츠는 다음 섹션에서 설명합니다.
앱 매니페스트에 FileProvider
를 추가한 후에는
공유하려는 파일이 포함된 디렉터리를 지정해야 합니다.
먼저 res/xml/
디렉터리에 filepaths.xml
파일을 만듭니다.
하위 디렉토리가 있습니다. 이 파일에서
확인할 수 있습니다 다음 스니펫은
res/xml/filepaths.xml
또한 이 스니펫은 하위 디렉터리를 공유하는 방법을 보여줍니다.
files/
디렉터리의 파일 이름을 지정합니다.
<paths>
<files-path path="images/" name="myimages" />
</paths>
이 예에서 <files-path>
태그는
앱 내부 저장소의 files/
디렉터리. path
속성
files/
의 images/
하위 디렉터리를 공유합니다. name
속성은 경로 세그먼트를 추가하도록 FileProvider
에 지시합니다.
files/images/
하위 디렉터리에 있는 파일의 콘텐츠 URI에 대한 myimages
<paths>
요소에는 여러 하위 요소가 있을 수 있으며 각 하위 요소는 서로 다른
공유할 수 있습니다. <files-path>
요소 외에도 다음 작업을 할 수 있습니다.
<external-path>
요소를 사용하여 외부 저장소의 디렉터리를 공유합니다.
<cache-path>
요소를 사용하여 내부 캐시의 디렉터리 공유
디렉터리 공유 디렉터리를 지정하는 하위 요소에 대해 자세히 알아보려면
FileProvider
참조 문서
참고: XML 파일을 사용해야만 원하는 디렉터리를 지정할 수 있습니다.
공유 프로그래매틱 방식으로 디렉터리를 추가할 수는 없습니다.
이제 FileProvider
의 완전한 사양이 생겼습니다.
앱의 files/
디렉터리에 있는 파일의 콘텐츠 URI를 생성하는
내부 저장소 또는 files/
의 하위 디렉터리에 있는 파일 앱에서
콘텐츠 URI에 있으며, 이 파일에는
요소 <provider>
개 (com.example.myapp.fileprovider
),
경로 myimages/
및 파일 이름.
예를 들어FileProvider
코드 스니펫에 대해 살펴보고
default_image.jpg
, FileProvider
는
다음 URI:
content://com.example.myapp.fileprovider/myimages/default_image.jpg
추가 관련 정보는 다음을 참조하세요.
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-27(UTC)
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-07-27(UTC)"],[],[],null,["# Setting up file sharing\n\nTo securely offer a file from your app to another app, you need to configure your app to offer\na secure handle to the file, in the form of a content URI. The Android\n[FileProvider](/reference/androidx/core/content/FileProvider) component generates content URIs for\nfiles, based on specifications you provide in XML. This lesson shows you how to add the default\nimplementation of [FileProvider](/reference/androidx/core/content/FileProvider) to your app, and how to\nspecify the files you want to offer to other apps.\n\n\n**Note:** The [FileProvider](/reference/androidx/core/content/FileProvider) class is part of the\n[AndroidX Core Library](/jetpack/androidx/releases/core). For information\nabout including this library in your application, see\n[Declaring dependencies](/jetpack/androidx/releases/core#declaring_dependencies).\n\nSpecify the FileProvider\n------------------------\n\n\nDefining a [FileProvider](/reference/androidx/core/content/FileProvider) for your app requires an entry in\nyour manifest. This entry specifies the authority to use in generating content URIs, as well as\nthe name of an XML file that specifies the directories your app can share.\n\n\nThe following snippet shows you how to add to your manifest the\n[\u003cprovider\u003e](/guide/topics/manifest/provider-element) element that specifies the\n[FileProvider](/reference/androidx/core/content/FileProvider) class, the authority, and the\nXML file name: \n\n```xml\n\u003cmanifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n package=\"com.example.myapp\"\u003e\n \u003capplication\n ...\u003e\n \u003cprovider\n android:name=\"androidx.core.content.FileProvider\"\n android:authorities=\"com.example.myapp.fileprovider\"\n android:grantUriPermissions=\"true\"\n android:exported=\"false\"\u003e\n \u003cmeta-data\n android:name=\"android.support.FILE_PROVIDER_PATHS\"\n android:resource=\"@xml/filepaths\" /\u003e\n \u003c/provider\u003e\n ...\n \u003c/application\u003e\n\u003c/manifest\u003e\n```\n\n\nIn this example, the [android:authorities](/guide/topics/manifest/provider-element#auth) attribute specifies the URI authority\nthat you want to use for content URIs generated by the\n[FileProvider](/reference/androidx/core/content/FileProvider).\nIn the example, the authority is `com.example.myapp.fileprovider`. For your own\napp, specify an authority consisting of the app's\n[android:package](/guide/topics/manifest/manifest-element#package) value with the string \"fileprovider\" appended to it. To learn more\nabout the authority value, see the topic\n[Content URIs](/guide/topics/providers/content-provider-basics#ContentURIs) and the documentation for the\n[android:authorities](/guide/topics/manifest/provider-element#auth) attribute.\n\n\nThe [\u003cmeta-data\u003e](/guide/topics/manifest/meta-data-element) child element of the\n[\u003cprovider\u003e](/guide/topics/manifest/provider-element) points to an XML file that specifies the directories you want to\nshare. The `android:resource` attribute is the path and name of the file, without\nthe `.xml` extension.The contents of this file are described in the next section.\n\nSpecify sharable directories\n----------------------------\n\n\nOnce you have added the [FileProvider](/reference/androidx/core/content/FileProvider) to your app manifest,\nyou need to specify the directories that contain the files you want to share. To specify the\ndirectories, start by creating the file `filepaths.xml` in the `res/xml/`\nsubdirectory of your project. In this file, specify the directories by adding an XML element for\neach directory. The following snippet shows you an example of the contents of\n`res/xml/filepaths.xml`. The snippet also demonstrates how to share a subdirectory\nof the `files/` directory in your internal storage area: \n\n```xml\n\u003cpaths\u003e\n \u003cfiles-path path=\"images/\" name=\"myimages\" /\u003e\n\u003c/paths\u003e\n```\n\n\nIn this example, the `\u003cfiles-path\u003e` tag shares directories within the\n`files/` directory of your app's internal storage. The `path` attribute\nshares the `images/` subdirectory of `files/`. The `name`\nattribute tells the [FileProvider](/reference/androidx/core/content/FileProvider) to add the path segment\n`myimages` to content URIs for files in the `files/images/` subdirectory.\n\n\nThe `\u003cpaths\u003e` element can have multiple children, each specifying a different\ndirectory to share. In addition to the `\u003cfiles-path\u003e` element, you can\nuse the `\u003cexternal-path\u003e` element to share directories in external storage, and\nthe `\u003ccache-path\u003e` element to share directories in your internal cache\ndirectory. To learn more about the child elements that specify shared directories, see the\n[FileProvider](/reference/androidx/core/content/FileProvider) reference documentation.\n\n\n**Note:** The XML file is the only way you can specify the directories you want to\nshare; you can't programmatically add a directory.\n\n\nYou now have a complete specification of a [FileProvider](/reference/androidx/core/content/FileProvider)\nthat generates content URIs for files in the `files/` directory of your app's\ninternal storage or for files in subdirectories of `files/`. When your app generates\na content URI for a file, it contains the authority specified in the\n[\u003cprovider\u003e](/guide/topics/manifest/provider-element) element (`com.example.myapp.fileprovider`),\nthe path `myimages/`, and the name of the file.\n\n\nFor example, if you define a [FileProvider](/reference/androidx/core/content/FileProvider) according to the\nsnippets in this lesson, and you request a content URI for the file\n`default_image.jpg`, [FileProvider](/reference/androidx/core/content/FileProvider) returns the\nfollowing URI: \n\n```\ncontent://com.example.myapp.fileprovider/myimages/default_image.jpg\n```\n\nFor additional related information, refer to:\n\n- [Storage Options](/guide/topics/data/data-storage)\n- [Saving Files](/training/basics/data-storage/files)"]]