Chia sẻ tệp
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Các ứng dụng thường có nhu cầu cung cấp một hoặc nhiều tệp cho một ứng dụng khác. Ví dụ: thư viện hình ảnh có thể muốn cung cấp tệp cho trình chỉnh sửa hình ảnh hoặc một ứng dụng quản lý tệp nên cho phép người dùng sao chép và dán tệp giữa các khu vực trong bộ nhớ ngoài. Ứng dụng gửi có thể chia sẻ tệp theo một cách là phản hồi yêu cầu của ứng dụng nhận.
Trong mọi trường hợp, cách an toàn duy nhất để cung cấp một tệp từ ứng dụng của bạn sang một ứng dụng khác là gửi cho ứng dụng nhận URI nội dung của tệp đó và cấp quyền truy cập tạm thời cho URI đó.
Các URI nội dung có quyền truy cập URI tạm thời đều an toàn vì những URI này chỉ áp dụng cho ứng dụng nhận URI và sẽ tự động hết hạn. Thành phần FileProvider
của Android cung cấp phương thức getUriForFile()
để tạo URI nội dung của tệp.
Nếu muốn chia sẻ một lượng nhỏ dữ liệu dạng văn bản hoặc số giữa các ứng dụng, bạn nên gửi Intent
chứa dữ liệu đó. Để tìm hiểu cách gửi dữ liệu đơn giản bằng Intent
, hãy xem lớp đào tạo Chia sẻ dữ liệu đơn giản.
Lớp này giải thích cách chia sẻ an toàn các tệp từ ứng dụng của bạn sang một ứng dụng khác bằng cách sử dụng URI nội dung do thành phần FileProvider
của Android tạo ra và các quyền tạm thời mà bạn cấp cho ứng dụng nhận đối với URI nội dung.
Bài học
- Thiết lập tính năng chia sẻ tệp
-
Tìm hiểu cách thiết lập ứng dụng để chia sẻ tệp.
- Chia sẻ tệp
-
Tìm hiểu cách cung cấp tệp cho một ứng dụng khác bằng cách tạo URI nội dung cho tệp,
cấp quyền truy cập vào URI và gửi URI cho ứng dụng.
- Yêu cầu một tệp được chia sẻ
-
Tìm hiểu cách yêu cầu tệp do một ứng dụng khác chia sẻ, nhận URI nội dung cho tệp và sử dụng URI nội dung để mở tệp.
-
Truy xuất thông tin tệp
-
Tìm hiểu cách một ứng dụng có thể dùng URI nội dung do
FileProvider
tạo để truy xuất thông tin tệp, bao gồm cả loại MIME và kích thước tệp.
Để biết thêm thông tin liên quan, hãy tham khảo:
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-27 UTC."],[],[],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)"]]