शेयर किए गए डेटासेट ऐक्सेस करना
संग्रह की मदद से व्यवस्थित रहें
अपनी प्राथमिकताओं के आधार पर, कॉन्टेंट को सेव करें और कैटगरी में बांटें.
Android 11 (एपीआई लेवल 30) से, सिस्टम बड़े डेटासेट को कैश मेमोरी में सेव करता है. ऐसा इसलिए किया जाता है, ताकि कई ऐप्लिकेशन मशीन लर्निंग और मीडिया चलाने जैसे इस्तेमाल के मामलों के लिए, इन डेटासेट को ऐक्सेस कर सकें. इस सुविधा से, नेटवर्क और डिस्क, दोनों पर डेटा की डुप्लीकेसी को कम करने में मदद मिलती है.
जब आपके ऐप्लिकेशन को शेयर किए गए बड़े डेटासेट का ऐक्सेस चाहिए होता है, तो वह सबसे पहले कैश मेमोरी में सेव किए गए इन डेटासेट को ढूंढ सकता है. इन्हें शेयर किए गए डेटा ब्लोब कहा जाता है. इसके बाद, वह यह तय करता है कि नई कॉपी डाउनलोड करनी है या नहीं. ऐप्लिकेशन, शेयर किए गए डेटासेट की इन सुविधाओं को ऐक्सेस कर सकते हैं. इसके लिए, उन्हें BlobStoreManager
में मौजूद एपीआई का इस्तेमाल करना होगा.
सिस्टम, शेयर किए गए डेटा के बड़े हिस्से को बनाए रखता है. साथ ही, यह कंट्रोल करता है कि कौनसे ऐप्लिकेशन उन्हें ऐक्सेस कर सकते हैं. जब आपका ऐप्लिकेशन डेटा ब्लोब का योगदान देता है, तब यह बताया जा सकता है कि किन अन्य ऐप्लिकेशन को डेटा का ऐक्सेस होना चाहिए. इसके लिए, इनमें से किसी एक तरीके का इस्तेमाल करें:
- किसी डिवाइस पर ऐप्लिकेशन के किसी खास सेट को ऐक्सेस करने की अनुमति देने के लिए, इन ऐप्लिकेशन के पैकेज के नाम
allowPackageAccess()
में पास करें.
- सिर्फ़ उन ऐप्लिकेशन को अनुमति दें जिनके सर्टिफ़िकेट, आपके ऐप्लिकेशन के लिए इस्तेमाल की गई कुंजी से साइन किए गए हैं. जैसे, आपके मैनेज किए जाने वाले ऐप्लिकेशन का सुइट. इसके लिए,
allowSameSignatureAccess()
को कॉल करें.
- किसी डिवाइस पर मौजूद सभी ऐप्लिकेशन का ऐक्सेस देने के लिए,
allowPublicAccess()
को कॉल करें.
शेयर किए गए डेटा ब्लोब ऐक्सेस करना
सिस्टम, शेयर किए गए हर डेटा ब्लोब को BlobHandle
ऑब्जेक्ट का इस्तेमाल करके दिखाता है. BlobHandle
के हर इंस्टेंस में, क्रिप्टोग्राफ़िक रूप से सुरक्षित हैश और डेटासेट की पहचान करने वाली कुछ जानकारी होती है.
शेयर किए गए डेटा ब्लोब को ऐक्सेस करने के लिए, सर्वर से पहचान करने वाली जानकारी डाउनलोड करें. इस जानकारी का इस्तेमाल करके, यह देखें कि डेटासेट सिस्टम पर पहले से उपलब्ध है या नहीं.
अगला चरण इस बात पर निर्भर करता है कि डेटा उपलब्ध है या नहीं.
डेटासेट उपलब्ध है
अगर डेटासेट डिवाइस पर पहले से मौजूद है, तो सिस्टम से इसे ऐक्सेस करें. इसके लिए, यहां दिए गए कोड स्निपेट का इस्तेमाल करें:
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 और/या इससे जुड़ी हुई कंपनियों के ट्रेडमार्क या रजिस्टर किए हुए ट्रेडमार्क हैं.
आखिरी बार 2025-08-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-08-27 (UTC) को अपडेट किया गया."],[],[],null,["Starting 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\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\nDataset 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\nKotlin \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\nJava \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\nDataset 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\nKotlin \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\nJava \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```"]]