ตั้งแต่วันที่ 27 มีนาคม 2025 เป็นต้นไป เราขอแนะนำให้ใช้ android-latest-release
แทน aosp-main
เพื่อสร้างและมีส่วนร่วมใน AOSP โปรดดูข้อมูลเพิ่มเติมที่หัวข้อการเปลี่ยนแปลงใน AOSP
โมดูล MediaProvider
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
โมดูล MediaProvider จะเพิ่มประสิทธิภาพข้อมูลเมตาที่จัดทําดัชนี (เสียง วิดีโอ และรูปภาพจากการ์ด SD และอุปกรณ์ USB) และทําให้แอปเข้าถึงข้อมูลดังกล่าวได้ผ่าน MediaStore public API
โมดูล MediaProvider จะบังคับใช้รูปแบบความปลอดภัยของพื้นที่เก็บข้อมูลแบบจำกัดที่เปิดตัวใน Android 10 เพื่อรักษาความเป็นส่วนตัวของผู้ใช้ ซึ่งรวมถึงการปกปิดข้อมูลเมตาตำแหน่งที่ละเอียดอ่อน
โมดูลนี้อัปเดตได้ ซึ่งช่วยให้ Android ตอบสนองต่อปัญหาด้านความปลอดภัยได้เร็วขึ้น (ปกป้องข้อมูลผู้ใช้ที่มีความละเอียดอ่อน) และเพิ่มรูปแบบสื่อใหม่ได้เร็วขึ้น (มอบความเสถียรให้กับทั้งผู้ใช้และนักพัฒนาแอป)
การเปลี่ยนแปลงใน Android 10
Android 10 มีการปรับปรุงหลายอย่างเกี่ยวกับการระบุและดึงข้อมูลจากไฟล์สื่อ โดยเฉพาะอย่างยิ่ง
การกำหนดประเภทเนื้อหาของไฟล์โดยใช้ส่วนแรกของประเภท MIME ของไฟล์
เช่น ระบบปฏิบัติการทราบว่าทั้ง image/png
และ image/x-newly-invented-format
เป็นรูปภาพ จึงอธิบายสิทธิ์ที่เกี่ยวข้องแก่ผู้ใช้ปลายทางได้อย่างถูกต้อง
การกำหนดประเภท MIME โดยใช้เฉพาะนามสกุลไฟล์ (และไม่ใช้การสแกนหาเนื้อหาเพื่อหลีกเลี่ยงปัญหาด้านความปลอดภัย)
การกำหนดประเภท MIME ของไฟล์ที่กำหนดเองโดยใช้การรวมการแมปจาก Debian Linux และ Android ต้นทาง
แสดงข้อมูลที่เกี่ยวข้องจากไฟล์ video/*
และ audio/*
(ผ่าน MediaMetadataRetriever
) และไฟล์ image/*
(ผ่าน ExifInterface
)
การเปลี่ยนแปลงใน Android 11
ใน Android 11 โมดูล MediaProvider จะต่อยอดจากการเปลี่ยนแปลงใน Android 10 ด้วยการปรับปรุงต่อไปนี้
การปรับปรุงการจัดทําดัชนี ตอนนี้โมดูล MediaProvider จะจัดทำดัชนีข้อมูลเมตาโดยทำการปรับยอดข้อมูลเมตาที่มีอยู่กับ MediaStore Public API การเปลี่ยนแปลงดังกล่าวได้แก่
คอลัมน์ is_favorite
ใหม่และอาร์กิวเมนต์ QUERY_ARG_MATCH_FAVORITE
เพื่อช่วยให้แอปสไตล์แกลเลอรีกรองสื่อตามคอลัมน์นี้ได้อย่างรวดเร็ว
การจัดทําดัชนีข้อมูลเมตาของพื้นที่สี
คอลัมน์ "is_trashed" ใหม่และอาร์กิวเมนต์ QUERY_ARG_MATCH_TRASHED
เพื่อเปิดใช้แอปสไตล์แกลเลอรีในการกรองตามคอลัมน์นี้
API ใหม่ที่ช่วยให้แก้ไขรายการหลายรายการพร้อมกันได้โดยใช้พรอมต์กล่องโต้ตอบของผู้ใช้รายการเดียว ซึ่งได้แก่ createDeleteRequest()
, createFavoriteRequest()
, createTrashRequest()
และ createWriteRequest()
คอลัมน์ GENERATION_ADDED
และ GENERATION_MODIFIED
ใหม่สำหรับใช้ในการตรวจหาการเปลี่ยนแปลงที่เกิดขึ้นตั้งแต่จุดซิงค์ก่อนหน้าได้อย่างรวดเร็วและเชื่อถือได้
GROUP BY
API สาธารณะใหม่สําหรับใช้กับคอลัมน์ข้อมูลเมตาเพิ่มเติมที่ไม่ได้กล่าวถึงข้างต้น
การปรับปรุง ExifInterface
เพื่อดึงข้อมูลเมตาจากคอนเทนเนอร์ PNG และ WebP
การปรับปรุง SystemUI
เพื่อเขียนข้อมูลเมตา DateTimeOriginal
ในการจับภาพหน้าจอ
นอกจากนี้ ตอนนี้คุณยังปรับแต่ง MediaProvider ได้ด้วยการเพิ่มรูปแบบสื่อใหม่ การทำเครื่องหมายอุปกรณ์เก็บข้อมูลที่ควรจัดทำดัชนี และแม้แต่การเปลี่ยนแพ็กเกจ MTP โปรดดูรายละเอียดที่หัวข้อการปรับแต่ง
ขอบเขตของโมดูล
Android 11 จะย้ายข้อมูลโค้ดทั้งหมดใน packages/providers/MediaProvider
ไปยังตำแหน่งใหม่ ยกเว้นตรรกะที่เกี่ยวข้องกับ MTP นอกจากนี้ ตอนนี้ frameworks/base/core/java/android/provider/MediaStore.java
อยู่ภายในขอบเขตของข้อบังคับที่ packages/providers/MediaProvider
โมดูล MediaProvider อยู่ในรูปแบบ APK-in-APEX
ทรัพยากร Dependency
Dependency ของ MediaProvider เกี่ยวข้องกับการปรับแต่ง (กล่าวคือ หากคุณปรับแต่ง MediaProvider คุณต้องตรวจสอบว่าการติดตั้งใช้งานเป็นไปตาม Dependency ที่เชื่อมโยงกับการปรับแต่งของคุณ)
เมื่อใช้รูปแบบไฟล์สื่อที่กำหนดเองหรือไม่เป็นมาตรฐาน (เช่น รูปแบบที่สร้างขึ้นโดยแอปกล้องเฉพาะของผู้ให้บริการ) คุณต้องลงทะเบียนรูปแบบที่กำหนดเองแต่ละรูปแบบกับ MimeUtils
และข้อบังคับของ Media Extractor เพื่อเปิดใช้การจัดทําดัชนีโดย MediaProvider
ตั้งค่า Flag VolumeInfo.MOUNT_FLAG_INDEXABLE
เพื่อให้ MediaProvider จัดทําดัชนีชุดอุปกรณ์เก็บข้อมูลที่กําหนดเอง (เช่น ช่องการ์ด SD และพอร์ต USB) ที่ใช้กับการติดตั้งใช้งาน StorageManagerService
เมื่อใช้การติดตั้งใช้งาน MTP ที่กําหนดเอง (ไม่ใช่ AOSP) ให้ตรวจสอบว่าการติดตั้งใช้งานใช้ API สาธารณะและ API ของระบบเพียงอย่างเดียวเพื่อให้การติดตั้งใช้งานโต้ตอบกับ MediaStore ได้
การปรับแต่ง
ตอนนี้คุณสามารถเพิ่มรูปแบบสื่อใหม่ กำหนดอุปกรณ์เก็บข้อมูลที่จะให้จัดทําดัชนี และแทนที่กอง MTP ได้แล้ว
รูปแบบสื่อที่กำหนดเอง สำหรับรูปแบบสื่อที่กำหนดเองใหม่แต่ละรูปแบบ คุณต้องระบุการแมปจากนามสกุลไฟล์ที่ไม่ซ้ำกันกับประเภท MIME เราขอแนะนําอย่างยิ่งให้คุณทําตามกระบวนการจดทะเบียน IANA
คุณไม่สามารถกำหนดนามสกุลหรือประเภท MIME ซ้ำซึ่งกำหนดไว้ใน AOSP อยู่แล้ว
สำหรับไฟล์ video/*
และ audio/*
ทาง MediaProvider จะยังคงให้คำปรึกษาต่อไป
MediaMetadataRetriever
ใช้เครื่องมือแยกสื่อของ Android 10 เพื่อแสดงข้อมูลเมตาสำหรับรูปแบบที่กำหนดเอง
สำหรับไฟล์ image/*
นั้น MediaProvider จะยังคงใช้ Exif
เป็นมาตรฐานสำหรับข้อมูลเมตา คุณสามารถขยาย android.media.ExifInterface
เพื่อดึงข้อมูลและแสดงผลข้อมูลเมตา Exif
สำหรับรูปแบบรูปภาพที่กำหนดเองได้
Flag การจัดทําดัชนีอุปกรณ์จัดเก็บข้อมูล MediaProvider จะจัดทำดัชนีสำหรับทุกเล่มที่ StorageManager.getStorageVolumes()
แสดงผล โดยที่ StorageVolume.getMediaStoreVolumeName()
ไม่ใช่ค่าว่าง คุณปรับแต่งรายการวอลุ่มที่แสดงผลเพื่อกำหนดสิ่งที่จะจัดทำดัชนีได้ แต่เราไม่แนะนำให้รวมวอลุ่มชั่วคราว (เช่น ไดรฟ์ USB OTG)
การเปลี่ยนสแต็ก MTP Android 11 จะวางกอง MTP ไว้นอกขอบเขตของโมดูลทั้งหมด และตรวจสอบว่าทำงานกับ API สาธารณะได้
การทดสอบ
คุณสามารถยืนยันฟังก์ชันการทำงานของ MediaProvider โดยใช้การทดสอบต่อไปนี้
หากต้องการยืนยันฟังก์ชันการทำงานของ MediaStore Public API ให้ใช้การทดสอบในแพ็กเกจ CtsProviderTestCases
ของชุดเครื่องมือทดสอบความเข้ากันได้ของ Android (CTS)
หากต้องการยืนยันฟังก์ชันการทํางานของ MediaProvider ภายใน ให้ใช้การทดสอบใน MediaProviderTests
หากต้องการเรียกใช้การทดสอบทั้ง 2 ชุดพร้อมกัน ให้ใช้คำสั่ง atest
ต่อไปนี้
atest --test-mapping packages/providers/MediaProvider
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ 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,["# MediaProvider module\n\nThe MediaProvider module optimizes indexed metadata (audio, video, and images\nfrom SD cards and USB devices) and makes that data available to apps through the\n[MediaStore public\nAPIs](https://developer.android.com/reference/android/provider/MediaStore).\nTo maintain user privacy, the MediaProvider module enforces the [scoped storage\nsecurity\nmodel](https://developer.android.com/training/data-storage/files/external-scoped)\nintroduced in Android 10, which includes redacting sensitive location metadata.\nThis module is updatable, enabling Android to respond faster to security issues\n(keeping sensitive user data protected) and add new media formats quicker\n(providing consistency to both users and developers).\n\nChanges in Android 10\n---------------------\n\nAndroid 10 introduced several improvements related to identifying and extracting\ndata from media files, specifically:\n\n- Determining the file content type using the first part of a file's MIME type.\n For example, the OS knows that both `image/png` and\n `image/x-newly-invented-format` are images, and can thus accurately describe\n relevant permissions to the end user.\n\n- Determining the MIME type using only the file extension (and without using\n [content sniffing](https://en.wikipedia.org/wiki/Content_sniffing)\n to avoid security issues).\n\n- Determining the MIME type of an arbitrary file using a combination of\n [upstream Debian Linux and Android\n mappings](https://android-review.googlesource.com/c/platform/libcore/+/735506).\n\n- Returning relevant data from `video/*` and `audio/*` files (via\n `MediaMetadataRetriever`) and `image/*` files (via `ExifInterface`).\n\nChanges in Android 11\n---------------------\n\nIn Android 11, the MediaProvider module builds on the\nchanges made in Android 10 with the following improvements:\n\n- Improvements to indexing. The MediaProvider module now indexes metadata by\n reconciling available metadata against MediaStore public APIs. Changes\n include:\n\n - New `is_favorite` column and `QUERY_ARG_MATCH_FAVORITE` argument to enable\n gallery-style apps to quickly filter media based on this column.\n\n - Indexing color space metadata.\n\n - New 'is_trashed' column and `QUERY_ARG_MATCH_TRASHED` argument to enable\n gallery-style apps to filter based on this column.\n\n - New APIs that enable bulk-modification of multiple items with a single user\n dialog prompt, including `createDeleteRequest()`, `createFavoriteRequest()`,\n `createTrashRequest()`, and `createWriteRequest()`.\n\n - New `GENERATION_ADDED` and `GENERATION_MODIFIED` columns for use in quickly\n and reliably detecting changes that have occurred since a previous\n synchronization point.\n\n - New `GROUP BY` public API for use with additional metadata columns not\n mentioned above.\n\n- Improvement to `ExifInterface` to extract metadata from PNG and WebP\n containers.\n\n- Improvements to `SystemUI` to write `DateTimeOriginal` metadata in screen\n captures.\n\nIn addition, you can now customize MediaProvider by adding new media formats,\nmarking which storage devices should be indexed, and even replacing the MTP\nstack. For details, see [Customization](#customization).\n\nModule boundary\n---------------\n\nAndroid 11 migrates all code in\n`packages/providers/MediaProvider` to a new location, with the notable exception\nof MTP-related logic. In addition,\n`frameworks/base/core/java/android/provider/MediaStore.java` is now *inside* the\nmodule boundary at `packages/providers/MediaProvider`.\n\nPackage format\n--------------\n\nThe MediaProvider module is in APK-in-APEX format.\n\nDependencies\n------------\n\nMediaProvider dependencies are related to [customizations](#customization) (that\nis, if you customize MediaProvider, you must ensure your implementation meets\nthe dependency associated with your customization).\n\n- When using custom or nonstandard media file formats (for example, a format\n generated by a vendor-specific Camera app), you must register each custom\n format with `MimeUtils` and the Media Extractor module to enable indexing by\n MediaProvider.\n\n- To ensure MediaProvider indexes a custom set of storage devices (such SD card\n slots and USB ports) used in a `StorageManagerService` implementation, set the\n `VolumeInfo.MOUNT_FLAG_INDEXABLE` flag.\n\n- When using a custom (non-AOSP) MTP implementation, ensure the implementation\n relies solely on public and system APIs to enable the implementation to\n interact with MediaStore.\n\nCustomization\n-------------\n\nYou can now add new media formats, influence which storage devices are indexed,\nand replace the MTP stack.\n\n- **Custom media formats.** For each new custom media format, you must provide a\n mapping from the unique file extension to a MIME type. We strongly encourage\n you to follow the [IANA registration\n process](https://www.iana.org/assignments/media-types/media-types.xhtml).\n\n - You can't redefine an extension or MIME type that's already defined in AOSP.\n\n - For `video/*` and `audio/*` files, MediaProvider continues consulting\n `MediaMetadataRetriever`. Use the Android 10 Media Extractors to return\n metadata for custom formats.\n\n - For `image/*` files, MediaProvider continues standardizing on `Exif` for\n metadata. You can extend `android.media.ExifInterface` to extract and return\n `Exif` metadata for any custom image formats.\n\n- **Storage devices indexing flag.** MediaProvider indexes all volumes returned\n by `StorageManager.getStorageVolumes()` where\n `StorageVolume.getMediaStoreVolumeName()` is non-null. You can customize the\n list of volumes returned to influence what is indexed, but we advise against\n including transient volumes (such as USB OTG drives).\n\n- **MTP stack replacement.** Android 11 places the MTP\n stack entirely outside the module boundary and ensures that it works against\n public APIs.\n\nTesting\n-------\n\nYou can verify the functionality of MediaProvider using the following tests:\n\n- To verify the functionality of MediaStore public APIs, use tests in the\n `CtsProviderTestCases` package of the Android Compatibility Test Suite (CTS).\n\n- To verify the functionality of MediaProvider internals, use tests in\n `MediaProviderTests`.\n\nTo run both sets of tests together, use the following `atest` command: \n\n atest --test-mapping packages/providers/MediaProvider"]]