分享檔案
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
應用程式經常需要將一或多個檔案提供給其他應用程式。舉例來說,圖庫可能想將檔案提供給圖片編輯器,或者檔案管理應用程式可能會讓使用者在外部儲存空間的區域之間複製及貼上檔案。傳送應用程式分享檔案的方法之一,就是回應接收應用程式的要求。
在所有情況下,如要將應用程式檔案提供給其他應用程式,唯一安全的方法是將接收端檔案的內容 URI 傳送給接收者,並授予該 URI 的臨時存取權限。具有臨時 URI 存取權限的內容 URI 安全無虞,因為僅適用於接收 URI 的應用程式,而且會自動過期。Android FileProvider
元件提供 getUriForFile()
方法,用於產生檔案的內容 URI。
如要在應用程式之間共用少量文字或數字資料,您應傳送包含資料的 Intent
。如要瞭解如何使用 Intent
傳送簡易資料,請參閱訓練課程的「分享簡單資料」。
這個類別說明如何使用 Android FileProvider
元件產生的內容 URI,以及您針對內容 URI 授予接收應用程式的臨時權限,安全地將應用程式中的檔案分享到其他應用程式。
課程
- 設定檔案共用機制
-
瞭解如何設定應用程式來分享檔案。
- 共用檔案
- 瞭解如何產生檔案的內容 URI、將存取權授予 URI,並將 URI 傳送至應用程式,藉此為其他應用程式提供檔案。
- 要求存取共用檔案
- 瞭解如何要求其他應用程式共用的檔案、接收檔案的內容 URI,以及使用內容 URI 開啟檔案。
-
擷取檔案資訊
- 瞭解應用程式如何使用
FileProvider
產生的內容 URI 擷取 MIME 類型和檔案大小等檔案資訊。
如需其他相關資訊,請參閱:
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","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 (世界標準時間)。"],[],[],null,["# Sharing files\n\nApps often have a need to offer one or more of their files to another app. For example, an image\ngallery may want to offer files to image editors, or a file management app may want to allow\nusers to copy and paste files between areas in external storage. One way a sending app can\nshare a file is to respond to a request from the receiving app.\n\n\nIn all cases, the only secure way to offer a file from your app to another app is to send the\nreceiving app the file's content URI and grant temporary access permissions to that URI.\nContent URIs with temporary URI access permissions are secure because they apply only to the\napp that receives the URI, and they expire automatically. The Android\n[FileProvider](/reference/androidx/core/content/FileProvider) component provides the method\n[getUriForFile()](/reference/androidx/core/content/FileProvider#getUriForFile(android.content.Context, java.lang.String, java.io.File)) for\ngenerating a file's content URI.\n\n\nIf you want to share small amounts of text or numeric data between apps, you should send an\n[Intent](/reference/android/content/Intent) that contains the data. To learn how to send simple data with an\n[Intent](/reference/android/content/Intent), see the training class\n[Sharing simple data](/training/sharing).\n\n\nThis class explains how to securely share files from your app to another app using content URIs\ngenerated by the Android [FileProvider](/reference/androidx/core/content/FileProvider) component and\ntemporary permissions that you grant to the receiving app for the content URI.\n\nLessons\n-------\n\n**[Setting up file sharing](/training/secure-file-sharing/setup-sharing)**\n:\n Learn how to set up your app to share files.\n\n**[Sharing a file](/training/secure-file-sharing/share-file)**\n:\n Learn how to offer a file to another app by generating a content URI for the file,\n granting access permissions to the URI, and sending the URI to the app.\n\n**[Requesting a shared file](/training/secure-file-sharing/request-file)**\n:\n Learn how to request a file shared by another app, receive the content URI for the file,\n and use the content URI to open the file.\n\n\n**[Retrieving file information](/training/secure-file-sharing/retrieve-info)**\n:\n Learn how an app can use a content URI generated by a\n [FileProvider](/reference/androidx/core/content/FileProvider) to retrieve file information including\n MIME type and file size.\n\nFor additional related information, refer to:\n\n- [Storage Options](/guide/topics/data/data-storage)\n- [Saving Files](/training/basics/data-storage/files)\n- [Sharing Simple Data](/training/sharing)"]]