การแชร์ไฟล์
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
แอปมักจำเป็นต้องเสนอไฟล์อย่างน้อย 1 รายการให้กับแอปอื่น เช่น แกลเลอรีรูปภาพอาจต้องการเสนอไฟล์ให้กับโปรแกรมแก้ไขรูปภาพ หรือแอปการจัดการไฟล์อาจต้องการอนุญาตให้ผู้ใช้คัดลอกและวางไฟล์ระหว่างพื้นที่ต่างๆ ในที่จัดเก็บข้อมูลภายนอก วิธีหนึ่งที่แอปส่งสามารถแชร์ไฟล์ได้คือการตอบกลับคำขอจากแอปที่รับ
ในทุกกรณี วิธีเดียวที่ปลอดภัยในการเสนอไฟล์จากแอปของคุณไปยังแอปอื่นคือการส่ง URI เนื้อหาของไฟล์ไปยังแอปที่รับ และมอบสิทธิ์การเข้าถึงชั่วคราวแก่ URI ดังกล่าว
URI ของเนื้อหาที่มีสิทธิ์เข้าถึง URI แบบชั่วคราวจะปลอดภัยเนื่องจากจะมีผลกับแอปที่ได้รับ URI เท่านั้น และสิทธิ์ดังกล่าวจะหมดอายุโดยอัตโนมัติ คอมโพเนนต์ FileProvider
ของ Android มีเมธอด getUriForFile()
สำหรับการสร้าง URI ของเนื้อหาไฟล์
หากต้องการแชร์ข้อความหรือข้อมูลตัวเลขจํานวนเล็กน้อยระหว่างแอป คุณควรส่ง Intent
ที่มีข้อมูล หากต้องการดูวิธีส่งข้อมูลอย่างง่ายด้วย Intent
โปรดดูชั้นเรียนการฝึกอบรมการแชร์ข้อมูลอย่างง่าย
คลาสนี้จะอธิบายวิธีแชร์ไฟล์จากแอปของคุณไปยังแอปอื่นอย่างปลอดภัยโดยใช้ URI เนื้อหาที่คอมโพเนนต์ FileProvider
ของ Android สร้างขึ้น และสิทธิ์ชั่วคราวที่คุณให้ไว้แก่แอปที่รับสำหรับ URI เนื้อหา
บทเรียน
- การตั้งค่าการแชร์ไฟล์
-
ดูวิธีตั้งค่าแอปเพื่อแชร์ไฟล์
- การแชร์ไฟล์
-
ดูวิธีเสนอไฟล์ให้แอปอื่นด้วยการสร้าง URI เนื้อหาสำหรับไฟล์ ให้สิทธิ์เข้าถึง URI และส่ง URI ไปยังแอป
- การขอไฟล์ที่แชร์
-
ดูวิธีขอไฟล์ที่แอปอื่นแชร์ รับ URI เนื้อหาของไฟล์ และใช้ URI เนื้อหาเพื่อเปิดไฟล์
-
การดึงข้อมูลไฟล์
-
ดูวิธีที่แอปสามารถใช้ URI ของเนื้อหาที่สร้างขึ้นโดย
FileProvider
เพื่อดึงข้อมูลไฟล์ ซึ่งรวมถึงประเภท MIME และขนาดไฟล์
ดูข้อมูลเพิ่มเติมที่เกี่ยวข้องได้ที่
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[null,null,["อัปเดตล่าสุด 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)"]]