访问共享数据集
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
从 Android 11(API 级别 30)开始,系统会缓存可供多个应用访问的大型数据集,为机器学习和媒体播放等用例提供支持。此功能有助于减少网络中和磁盘上的数据冗余。
当您的应用需要访问大型共享数据集时,可以先查找是否有这类缓存的数据集(称为共享数据 blob),然后再决定是否下载新副本。应用可以通过 BlobStoreManager
中的 API 访问此共享数据集功能。
系统维护着共享数据 blob 并控制着哪些应用可以访问它们。当您的应用提供数据 blob 时,您可以通过调用以下方法之一指明应该对其具有访问权限的其他应用:
访问共享数据 blob
系统使用 BlobHandle
对象表示每个共享数据 blob。每个 BlobHandle
实例都包含相应数据集的加密安全哈希值和一些识别性详细信息。
如需访问共享数据 blob,请从服务器下载识别性详细信息。使用这些详细信息检查系统上是否已存在相应数据集。
下一步取决于是否已存在相应数据。
已存在数据集
如果设备上已存在相应数据集,请从系统访问该数据集,如以下代码段所示:
Kotlin
val blobStoreManager =
getSystemService(Context.BLOB_STORE_SERVICE) as BlobStoreManager
// The label "Sample photos" is visible to the user.
val blobHandle = BlobHandle.createWithSha256(sha256DigestBytes,
"Sample photos",
System.currentTimeMillis() + TimeUnit.DAYS.toMillis(1),
"photoTrainingDataset")
try {
val input = ParcelFileDescriptor.AutoCloseInputStream(
blobStoreManager.openBlob(blobHandle))
useDataset(input)
}
Java
BlobStoreManager blobStoreManager =
((BlobStoreManager) getSystemService(Context.BLOB_STORE_SERVICE));
if (blobStoreManager != null) {
// The label "Sample photos" is visible to the user.
BlobHandle blobHandle = BlobHandle.createWithSha256(
sha256DigestBytes,
"Sample photos",
System.currentTimeMillis() + TimeUnit.DAYS.toMillis(1),
"photoTrainingDataset");
try (InputStream input = new ParcelFileDescriptor.AutoCloseInputStream(
blobStoreManager.openBlob(blobHandle))) {
useDataset(input);
}
}
不存在数据集
如果不存在相应数据集,请从服务器下载该数据集并将其提供给系统,如以下代码段所示:
Kotlin
val sessionId = blobStoreManager.createSession(blobHandle)
try {
val session = blobStoreManager.openSession(sessionId)
try {
// For this example, write 200 MiB at the beginning of the file.
val output = ParcelFileDescriptor.AutoCloseOutputStream(
session.openWrite(0, 1024 * 1024 * 200))
writeDataset(output)
session.apply {
allowSameSignatureAccess()
allowPackageAccess(your-app-package,
app-certificate)
allowPackageAccess(some-other-app-package,
app-certificate)
commit(mainExecutor, callback)
}
}
}
Java
long sessionId = blobStoreManager.createSession(blobHandle);
try (BlobStoreManager.Session session =
blobStoreManager.openSession(sessionId)) {
// For this example, write 200 MiB at the beginning of the file.
try (OutputStream output = new ParcelFileDescriptor.AutoCloseOutputStream(
session.openWrite(0, 1024 * 1024 * 200)))
writeDataset(output);
session.allowSameSignatureAccess();
session.allowPackageAccess(your-app-package,
app-certificate);
session.allowPackageAccess(some-other-app-package,
app-certificate);
session.commit(getMainExecutor(), callback);
}
}
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Access shared datasets\n\nStarting in Android 11 (API level 30), the system caches large datasets that\nmultiple apps might access for use cases like machine learning and media\nplayback. This functionality helps reduce data redundancy, both over the network\nand on disk.\n\nWhen your app needs access to a shared large dataset, it can first look for\nthese cached datasets, called *shared data blobs* , before determining whether to\ndownload a new copy. Apps can access these shared datasets functionality using\nthe APIs in [`BlobStoreManager`](/reference/android/app/blob/BlobStoreManager).\n\nThe system maintains the shared data blobs and controls which apps can access\nthem. When your app contributes data blobs, you can indicate which other apps\nshould have access by calling one of the following methods:\n\n- To grant access to a specific set of apps on a device, pass the package names of these apps into [`allowPackageAccess()`](/reference/android/app/blob/BlobStoreManager.Session#allowPackageAccess(java.lang.String,%20byte%5B%5D)).\n- To allow only apps whose certificates are signed using the same key as the one used for your app---such as an app suite that you manage---call [`allowSameSignatureAccess()`](/reference/android/app/blob/BlobStoreManager.Session#allowSameSignatureAccess()).\n- To grant access to all apps on a device, call [`allowPublicAccess()`](/reference/android/app/blob/BlobStoreManager.Session#allowPublicAccess()).\n\nAccess shared data blobs\n------------------------\n\nThe system represents each shared data blob using a\n[`BlobHandle`](/reference/android/app/blob/BlobHandle) object. Each instance of `BlobHandle`\ncontains a cryptographically-secure hash and some identifying details for the\ndataset.\n\nTo access shared data blobs, download identifying details from the server. Using\nthese details, check whether the dataset is already available on the system.\n\nThe next step depends on whether data is available.\n\n### Dataset available\n\nIf the dataset is already available on the device, then access it from the system,\nas shown in the following code snippet: \n\n### Kotlin\n\n```kotlin\nval blobStoreManager =\n getSystemService(Context.BLOB_STORE_SERVICE) as BlobStoreManager\n// The label \"Sample photos\" is visible to the user.\nval blobHandle = BlobHandle.createWithSha256(sha256DigestBytes,\n \"Sample photos\",\n System.currentTimeMillis() + TimeUnit.DAYS.toMillis(1),\n \"photoTrainingDataset\")\ntry {\n val input = ParcelFileDescriptor.AutoCloseInputStream(\n blobStoreManager.openBlob(blobHandle))\n useDataset(input)\n}\n```\n\n### Java\n\n```java\nBlobStoreManager blobStoreManager =\n ((BlobStoreManager) getSystemService(Context.BLOB_STORE_SERVICE));\nif (blobStoreManager != null) {\n // The label \"Sample photos\" is visible to the user.\n BlobHandle blobHandle = BlobHandle.createWithSha256(\n sha256DigestBytes,\n \"Sample photos\",\n System.currentTimeMillis() + TimeUnit.DAYS.toMillis(1),\n \"photoTrainingDataset\");\n try (InputStream input = new ParcelFileDescriptor.AutoCloseInputStream(\n blobStoreManager.openBlob(blobHandle))) {\n useDataset(input);\n }\n}\n```\n\n### Dataset unavailable\n\nIf the dataset isn't available, then download it from the server and contribute it\nto the system, as shown in the following code snippet: \n\n### Kotlin\n\n```kotlin\nval sessionId = blobStoreManager.createSession(blobHandle)\ntry {\n val session = blobStoreManager.openSession(sessionId)\n try {\n // For this example, write 200 MiB at the beginning of the file.\n val output = ParcelFileDescriptor.AutoCloseOutputStream(\n session.openWrite(0, 1024 * 1024 * 200))\n writeDataset(output)\n\n session.apply {\n allowSameSignatureAccess()\n allowPackageAccess(your-app-package,\n app-certificate)\n allowPackageAccess(some-other-app-package,\n app-certificate)\n commit(mainExecutor, callback)\n }\n }\n}\n```\n\n### Java\n\n```java\nlong sessionId = blobStoreManager.createSession(blobHandle);\ntry (BlobStoreManager.Session session =\n blobStoreManager.openSession(sessionId)) {\n // For this example, write 200 MiB at the beginning of the file.\n try (OutputStream output = new ParcelFileDescriptor.AutoCloseOutputStream(\n session.openWrite(0, 1024 * 1024 * 200)))\n writeDataset(output);\n session.allowSameSignatureAccess();\n session.allowPackageAccess(your-app-package,\n app-certificate);\n session.allowPackageAccess(some-other-app-package,\n app-certificate);\n session.commit(getMainExecutor(), callback);\n }\n}\n```"]]