Kể từ ngày 27 tháng 3 năm 2025, bạn nên sử dụng android-latest-release
thay vì aosp-main
để xây dựng và đóng góp cho AOSP. Để biết thêm thông tin, hãy xem phần Thay đổi đối với AOSP.
Mô-đun MediaProvider
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.
Mô-đun MediaProvider tối ưu hoá siêu dữ liệu được lập chỉ mục (âm thanh, video và hình ảnh từ thẻ SD và thiết bị USB) và cung cấp dữ liệu đó cho các ứng dụng thông qua API công khai MediaStore.
Để duy trì quyền riêng tư của người dùng, mô-đun MediaProvider thực thi mô hình bảo mật bộ nhớ có giới hạn được giới thiệu trong Android 10, bao gồm cả việc loại bỏ siêu dữ liệu vị trí nhạy cảm.
Mô-đun này có thể cập nhật, cho phép Android phản hồi nhanh hơn các vấn đề bảo mật (bảo vệ dữ liệu nhạy cảm của người dùng) và thêm các định dạng nội dung nghe nhìn mới nhanh hơn (cung cấp tính nhất quán cho cả người dùng và nhà phát triển).
Các thay đổi trong Android 10
Android 10 đã giới thiệu một số điểm cải tiến liên quan đến việc xác định và trích xuất dữ liệu từ các tệp phương tiện, cụ thể:
Xác định loại nội dung tệp bằng cách sử dụng phần đầu của loại MIME của tệp.
Ví dụ: hệ điều hành biết rằng cả image/png
và image/x-newly-invented-format
đều là hình ảnh, do đó có thể mô tả chính xác các quyền liên quan cho người dùng cuối.
Xác định loại MIME chỉ bằng phần mở rộng tệp (và không sử dụng tính năng quét nội dung để tránh các vấn đề bảo mật).
Xác định loại MIME của một tệp tuỳ ý bằng cách kết hợp các ánh xạ ngược dòng của Debian Linux và Android.
Trả về dữ liệu có liên quan từ các tệp video/*
và audio/*
(thông qua MediaMetadataRetriever
) và tệp image/*
(thông qua ExifInterface
).
Các thay đổi trong Android 11
Trong Android 11, mô-đun MediaProvider dựa trên các thay đổi đã thực hiện trong Android 10 với các điểm cải tiến sau:
Cải tiến tính năng lập chỉ mục. Mô-đun MediaProvider hiện lập chỉ mục siêu dữ liệu bằng cách điều chỉnh siêu dữ liệu có sẵn với các API công khai của MediaStore. Các thay đổi bao gồm:
Cột is_favorite
và đối số QUERY_ARG_MATCH_FAVORITE
mới để cho phép các ứng dụng kiểu thư viện nhanh chóng lọc nội dung nghe nhìn dựa trên cột này.
Lập chỉ mục siêu dữ liệu về không gian màu.
Cột "is_trashed" mới và đối số QUERY_ARG_MATCH_TRASHED
để cho phép các ứng dụng kiểu thư viện lọc dựa trên cột này.
Các API mới cho phép sửa đổi hàng loạt nhiều mục bằng một lời nhắc hộp thoại người dùng, bao gồm createDeleteRequest()
, createFavoriteRequest()
, createTrashRequest()
và createWriteRequest()
.
Các cột GENERATION_ADDED
và GENERATION_MODIFIED
mới để sử dụng trong việc phát hiện nhanh và đáng tin cậy các thay đổi đã xảy ra kể từ điểm đồng bộ hoá trước đó.
API công khai GROUP BY
mới để sử dụng với các cột siêu dữ liệu bổ sung không được đề cập ở trên.
Cải tiến ExifInterface
để trích xuất siêu dữ liệu từ các vùng chứa PNG và WebP.
Cải tiến SystemUI
để ghi siêu dữ liệu DateTimeOriginal
trong ảnh chụp màn hình.
Ngoài ra, giờ đây, bạn có thể tuỳ chỉnh MediaProvider bằng cách thêm các định dạng nội dung nghe nhìn mới, đánh dấu thiết bị lưu trữ cần được lập chỉ mục và thậm chí thay thế ngăn xếp MTP. Để biết thông tin chi tiết, hãy xem phần Tuỳ chỉnh.
Ranh giới mô-đun
Android 11 di chuyển tất cả mã trong packages/providers/MediaProvider
sang một vị trí mới, ngoại trừ logic liên quan đến MTP. Ngoài ra, frameworks/base/core/java/android/provider/MediaStore.java
hiện ở bên trong ranh giới mô-đun tại packages/providers/MediaProvider
.
Mô-đun MediaProvider ở định dạng APK-in-APEX.
Phần phụ thuộc
Các phần phụ thuộc MediaProvider liên quan đến các tuỳ chỉnh (tức là nếu tuỳ chỉnh MediaProvider, bạn phải đảm bảo việc triển khai của mình đáp ứng phần phụ thuộc liên quan đến tuỳ chỉnh đó).
Khi sử dụng định dạng tệp phương tiện tuỳ chỉnh hoặc không chuẩn (ví dụ: định dạng do ứng dụng Máy ảnh dành riêng cho nhà cung cấp tạo), bạn phải đăng ký từng định dạng tuỳ chỉnh với MimeUtils
và mô-đun Trình trích xuất nội dung nghe nhìn để bật tính năng lập chỉ mục theo MediaProvider.
Để đảm bảo MediaProvider lập chỉ mục một nhóm tuỳ chỉnh các thiết bị lưu trữ (chẳng hạn như khe thẻ SD và cổng USB) được dùng trong quá trình triển khai StorageManagerService
, hãy đặt cờ VolumeInfo.MOUNT_FLAG_INDEXABLE
.
Khi sử dụng phương thức triển khai MTP tuỳ chỉnh (không phải AOSP), hãy đảm bảo phương thức triển khai chỉ dựa vào các API công khai và hệ thống để cho phép phương thức triển khai tương tác với MediaStore.
Tuỳ chỉnh
Giờ đây, bạn có thể thêm các định dạng nội dung nghe nhìn mới, ảnh hưởng đến việc thiết bị lưu trữ nào được lập chỉ mục và thay thế ngăn xếp MTP.
Định dạng nội dung nghe nhìn tuỳ chỉnh. Đối với mỗi định dạng nội dung nghe nhìn tuỳ chỉnh mới, bạn phải cung cấp một mối liên kết từ đuôi tệp duy nhất đến một loại MIME. Bạn nên làm theo quy trình đăng ký với IANA.
Bạn không thể xác định lại một tiện ích hoặc loại MIME đã được xác định trong AOSP.
Đối với các tệp video/*
và audio/*
, MediaProvider tiếp tục tham khảo ý kiến của MediaMetadataRetriever
. Sử dụng Trình trích xuất nội dung đa phương tiện Android 10 để trả về siêu dữ liệu cho các định dạng tuỳ chỉnh.
Đối với tệp image/*
, MediaProvider tiếp tục chuẩn hoá trên Exif
cho siêu dữ liệu. Bạn có thể mở rộng android.media.ExifInterface
để trích xuất và trả về siêu dữ liệu Exif
cho mọi định dạng hình ảnh tuỳ chỉnh.
Cờ lập chỉ mục thiết bị lưu trữ. MediaProvider lập chỉ mục tất cả các phương tiện do StorageManager.getStorageVolumes()
trả về, trong đó StorageVolume.getMediaStoreVolumeName()
không phải là giá trị rỗng. Bạn có thể tuỳ chỉnh danh sách các phương tiện được trả về để ảnh hưởng đến nội dung được lập chỉ mục, nhưng bạn không nên đưa các phương tiện tạm thời (chẳng hạn như ổ USB OTG) vào.
Thay thế ngăn xếp MTP. Android 11 đặt ngăn xếp MTP hoàn toàn bên ngoài ranh giới mô-đun và đảm bảo rằng ngăn xếp này hoạt động với các API công khai.
Thử nghiệm
Bạn có thể xác minh chức năng của MediaProvider bằng các kiểm thử sau:
Để xác minh chức năng của các API công khai MediaStore, hãy sử dụng các chương trình kiểm thử trong gói CtsProviderTestCases
của Bộ kiểm thử tính tương thích với Android (CTS).
Để xác minh chức năng của các thành phần nội bộ MediaProvider, hãy sử dụng các bài kiểm thử trong MediaProviderTests
.
Để chạy cả hai bộ kiểm thử cùng nhau, hãy sử dụng lệnh atest
sau:
atest --test-mapping packages/providers/MediaProvider
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,["# 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"]]