Android 4.4 (एपीआई लेवल 19) और इसके बाद के वर्शन वाले डिवाइसों पर, आपका ऐप्लिकेशन Storage Access Framework का इस्तेमाल करके, documents provider के साथ इंटरैक्ट कर सकता है. इसमें बाहरी स्टोरेज वॉल्यूम और क्लाउड-आधारित स्टोरेज शामिल है. इस फ़्रेमवर्क की मदद से, उपयोगकर्ता सिस्टम पिकर के साथ इंटरैक्ट कर सकते हैं. इससे वे दस्तावेज़ उपलब्ध कराने वाली कंपनी चुन सकते हैं. साथ ही, आपके ऐप्लिकेशन के लिए खास दस्तावेज़ और अन्य फ़ाइलें चुन सकते हैं, ताकि ऐप्लिकेशन उन्हें बना सके, खोल सके या उनमें बदलाव कर सके.
इस तरीके में, उपयोगकर्ता उन फ़ाइलों या डायरेक्ट्री को चुनता है जिन्हें आपका ऐप्लिकेशन ऐक्सेस कर सकता है. इसलिए, इस तरीके के लिए किसी सिस्टम अनुमतियों की ज़रूरत नहीं होती. साथ ही, इससे उपयोगकर्ता को बेहतर कंट्रोल मिलता है और उसकी निजता भी सुरक्षित रहती है. इसके अलावा, ये फ़ाइलें किसी ऐप्लिकेशन के लिए बनी डायरेक्ट्री और मीडिया स्टोर के बाहर सेव की जाती हैं. इसलिए, ऐप्लिकेशन को अनइंस्टॉल करने के बाद भी ये डिवाइस पर बनी रहती हैं.
फ़्रेमवर्क का इस्तेमाल करने के लिए, यह तरीका अपनाएं:
- कोई ऐप्लिकेशन, स्टोरेज से जुड़ी कार्रवाई करने वाले इंटेंट को शुरू करता है. यह कार्रवाई, इस्तेमाल के किसी खास उदाहरण से जुड़ी है. फ़्रेमवर्क इसे उपलब्ध कराता है.
- उपयोगकर्ता को सिस्टम पिकर दिखता है. इससे वह दस्तावेज़ उपलब्ध कराने वाली सेवा को ब्राउज़ कर सकता है. साथ ही, वह ऐसी जगह या दस्तावेज़ चुन सकता है जहां स्टोरेज से जुड़ी कार्रवाई की जाती है.
- ऐप्लिकेशन को उस यूआरआई का ऐक्सेस मिल जाता है जो उपयोगकर्ता की चुनी गई जगह या दस्तावेज़ को दिखाता है. ऐप्लिकेशन इस यूआरआई को पढ़ और लिख सकता है. इस यूआरआई का इस्तेमाल करके, ऐप्लिकेशन चुनी गई जगह की जानकारी पर कार्रवाई कर सकता है.
Android 9 (एपीआई लेवल 28) या इससे पहले के वर्शन वाले डिवाइसों पर मीडिया फ़ाइल ऐक्सेस करने की सुविधा देने के लिए, READ_EXTERNAL_STORAGE
अनुमति का एलान करें और maxSdkVersion
को 28
पर सेट करें.
इस गाइड में, फ़ाइलों और अन्य दस्तावेज़ों के साथ काम करने के लिए, फ़्रेमवर्क के साथ काम करने वाले अलग-अलग इस्तेमाल के उदाहरण दिए गए हैं. इसमें यह भी बताया गया है कि उपयोगकर्ता की चुनी गई जगह की जानकारी पर कार्रवाइयां कैसे की जाती हैं.
दस्तावेज़ों और अन्य फ़ाइलों को ऐक्सेस करने के उदाहरण
Storage Access Framework, फ़ाइलों और अन्य दस्तावेज़ों को ऐक्सेस करने के लिए, इन मामलों में काम करता है.
- नई फ़ाइल बनाना
ACTION_CREATE_DOCUMENT
इंटेंट ऐक्शन की मदद से, उपयोगकर्ता किसी फ़ाइल को किसी खास जगह पर सेव कर सकते हैं.- कोई दस्तावेज़ या फ़ाइल खोलना
ACTION_OPEN_DOCUMENT
इंटेंट ऐक्शन की मदद से, उपयोगकर्ता खोलने के लिए कोई खास दस्तावेज़ या फ़ाइल चुन सकते हैं.- किसी डायरेक्ट्री के कॉन्टेंट का ऐक्सेस देना
- Android 5.0 (एपीआई लेवल 21) और इसके बाद के वर्शन पर उपलब्ध,
ACTION_OPEN_DOCUMENT_TREE
इंटेंट ऐक्शन की मदद से, उपयोगकर्ता किसी डायरेक्ट्री को चुन सकते हैं. इससे आपके ऐप्लिकेशन को उस डायरेक्ट्री में मौजूद सभी फ़ाइलों और सब-डायरेक्ट्री का ऐक्सेस मिल जाता है.
यहां दिए गए सेक्शन में, इस्तेमाल के हर उदाहरण को कॉन्फ़िगर करने के तरीके के बारे में बताया गया है.
नई फ़ाइल बनाना
सिस्टम फ़ाइल पिकर को लोड करने के लिए, ACTION_CREATE_DOCUMENT
इंटेंट ऐक्शन का इस्तेमाल करें. साथ ही, उपयोगकर्ता को वह जगह चुनने की अनुमति दें जहां फ़ाइल का कॉन्टेंट लिखना है. यह प्रोसेस, "इस रूप में सेव करें" डायलॉग बॉक्स में इस्तेमाल की जाने वाली प्रोसेस जैसी ही है. इस डायलॉग बॉक्स का इस्तेमाल अन्य ऑपरेटिंग सिस्टम करते हैं.
ध्यान दें: ACTION_CREATE_DOCUMENT
किसी मौजूदा फ़ाइल को ओवरराइट नहीं कर सकता. अगर आपका ऐप्लिकेशन एक ही नाम की फ़ाइल सेव करने की कोशिश करता है, तो सिस्टम फ़ाइल के नाम के आखिर में, ब्रैकेट में एक नंबर जोड़ देता है.
उदाहरण के लिए, अगर आपका ऐप्लिकेशन confirmation.pdf
नाम की फ़ाइल को ऐसी डायरेक्ट्री में सेव करने की कोशिश करता है जिसमें पहले से ही उस नाम की फ़ाइल मौजूद है, तो सिस्टम नई फ़ाइल को confirmation(1).pdf
नाम से सेव करता है.
इंटेंट कॉन्फ़िगर करते समय, फ़ाइल का नाम और MIME टाइप तय करें. इसके अलावा, EXTRA_INITIAL_URI
इंटेंट एक्स्ट्रा का इस्तेमाल करके, उस फ़ाइल या डायरेक्ट्री का यूआरआई तय करें जिसे फ़ाइल पिकर को पहली बार लोड होने पर दिखाना चाहिए.
यहां दिए गए कोड स्निपेट में, फ़ाइल बनाने के लिए इंटेंट बनाने और उसे शुरू करने का तरीका बताया गया है:
Kotlin
// Request code for creating a PDF document. const val CREATE_FILE = 1 private fun createFile(pickerInitialUri: Uri) { val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply { addCategory(Intent.CATEGORY_OPENABLE) type = "application/pdf" putExtra(Intent.EXTRA_TITLE, "invoice.pdf") // Optionally, specify a URI for the directory that should be opened in // the system file picker before your app creates the document. putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri) } startActivityForResult(intent, CREATE_FILE) }
Java
// Request code for creating a PDF document. private static final int CREATE_FILE = 1; private void createFile(Uri pickerInitialUri) { Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("application/pdf"); intent.putExtra(Intent.EXTRA_TITLE, "invoice.pdf"); // Optionally, specify a URI for the directory that should be opened in // the system file picker when your app creates the document. intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri); startActivityForResult(intent, CREATE_FILE); }
फ़ाइल खोलें
आपका ऐप्लिकेशन, दस्तावेज़ों को स्टोरेज यूनिट के तौर पर इस्तेमाल कर सकता है. इनमें उपयोगकर्ता ऐसा डेटा डालते हैं जिसे वे अपने साथियों के साथ शेयर करना चाहते हैं या जिसे वे दूसरे दस्तावेज़ों में इंपोर्ट करना चाहते हैं. इसके कई उदाहरण हैं. जैसे, कोई उपयोगकर्ता किसी प्रॉडक्टिविटी दस्तावेज़ को खोलता है या EPUB फ़ाइल के तौर पर सेव की गई किसी किताब को खोलता है.
ऐसे मामलों में, उपयोगकर्ता को ACTION_OPEN_DOCUMENT
इंटेंट शुरू करके, खोलने के लिए फ़ाइल चुनने की अनुमति दें. इससे सिस्टम का फ़ाइल पिकर ऐप्लिकेशन खुल जाता है. सिर्फ़ उन फ़ाइल टाइप को दिखाने के लिए जिन्हें आपका ऐप्लिकेशन इस्तेमाल कर सकता है, MIME टाइप तय करें. इसके अलावा, EXTRA_INITIAL_URI
इंटेंट एक्स्ट्रा का इस्तेमाल करके, उस फ़ाइल का यूआरआई भी तय किया जा सकता है जिसे फ़ाइल पिकर को पहली बार लोड होने पर दिखाना चाहिए.
नीचे दिए गए कोड स्निपेट में, PDF दस्तावेज़ खोलने के लिए इंटेंट बनाने और उसे शुरू करने का तरीका बताया गया है:
Kotlin
// Request code for selecting a PDF document. const val PICK_PDF_FILE = 2 fun openFile(pickerInitialUri: Uri) { val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply { addCategory(Intent.CATEGORY_OPENABLE) type = "application/pdf" // Optionally, specify a URI for the file that should appear in the // system file picker when it loads. putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri) } startActivityForResult(intent, PICK_PDF_FILE) }
Java
// Request code for selecting a PDF document. private static final int PICK_PDF_FILE = 2; private void openFile(Uri pickerInitialUri) { Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("application/pdf"); // Optionally, specify a URI for the file that should appear in the // system file picker when it loads. intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri); startActivityForResult(intent, PICK_PDF_FILE); }
ऐक्सेस से जुड़ी पाबंदियां
Android 11 (एपीआई लेवल 30) और इसके बाद के वर्शन पर, ACTION_OPEN_DOCUMENT
इंटेंट ऐक्शन का इस्तेमाल करके, उपयोगकर्ता से इन डायरेक्ट्री से अलग-अलग फ़ाइलें चुनने का अनुरोध नहीं किया जा सकता:
Android/data/
डायरेक्ट्री और सभी सबडायरेक्ट्री.Android/obb/
डायरेक्ट्री और सभी सबडायरेक्ट्री.
किसी डायरेक्ट्री के कॉन्टेंट का ऐक्सेस देना
फ़ाइल मैनेजमेंट और मीडिया बनाने वाले ऐप्लिकेशन, आम तौर पर फ़ाइलों के ग्रुप को डायरेक्ट्री के क्रम में मैनेज करते हैं. अपने ऐप्लिकेशन में यह सुविधा देने के लिए, ACTION_OPEN_DOCUMENT_TREE
इंटेंट ऐक्शन का इस्तेमाल करें. इससे उपयोगकर्ता को पूरी डायरेक्ट्री ट्री का ऐक्सेस देने की अनुमति मिलती है. हालांकि, Android 11 (एपीआई लेवल 30) से कुछ अपवाद शुरू होते हैं. इसके बाद, आपका ऐप्लिकेशन चुनी गई डायरेक्ट्री और उसकी किसी भी सब-डायरेक्ट्री में मौजूद किसी भी फ़ाइल को ऐक्सेस कर सकता है.
ACTION_OPEN_DOCUMENT_TREE
का इस्तेमाल करते समय, आपका ऐप्लिकेशन सिर्फ़ उस डायरेक्ट्री में मौजूद फ़ाइलों को ऐक्सेस कर सकता है जिसे उपयोगकर्ता चुनता है. आपके पास उन फ़ाइलों का ऐक्सेस नहीं है जो उपयोगकर्ता की चुनी गई इस डायरेक्ट्री के बाहर मौजूद हैं. उपयोगकर्ता के कंट्रोल में रहने वाले इस ऐक्सेस से, उपयोगकर्ता यह चुन सकते हैं कि उन्हें आपके ऐप्लिकेशन के साथ कौनसा कॉन्टेंट शेयर करना है.
EXTRA_INITIAL_URI
इंटेंट एक्स्ट्रा का इस्तेमाल करके, उस डायरेक्ट्री का यूआरआई तय किया जा सकता है जिसे फ़ाइल पिकर को पहली बार लोड होने पर दिखाना चाहिए. हालांकि, ऐसा करना ज़रूरी नहीं है.
यहां दिए गए कोड स्निपेट में, किसी डायरेक्ट्री को खोलने के लिए इंटेंट बनाने और उसे शुरू करने का तरीका बताया गया है:
Kotlin
fun openDirectory(pickerInitialUri: Uri) { // Choose a directory using the system's file picker. val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply { // Optionally, specify a URI for the directory that should be opened in // the system file picker when it loads. putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri) } startActivityForResult(intent, your-request-code) }
Java
public void openDirectory(Uri uriToLoad) { // Choose a directory using the system's file picker. Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); // Optionally, specify a URI for the directory that should be opened in // the system file picker when it loads. intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uriToLoad); startActivityForResult(intent, your-request-code); }
ऐक्सेस से जुड़ी पाबंदियां
Android 11 (एपीआई लेवल 30) और इसके बाद के वर्शन पर, इन डायरेक्ट्री का ऐक्सेस पाने के लिए, ACTION_OPEN_DOCUMENT_TREE
इंटेंट ऐक्शन का इस्तेमाल नहीं किया जा सकता:
- यह इंटरनल स्टोरेज वॉल्यूम की रूट डायरेक्ट्री होती है.
- हर एसडी कार्ड वॉल्यूम की रूट डायरेक्ट्री, जिसे डिवाइस बनाने वाली कंपनी भरोसेमंद मानती है. भले ही, कार्ड को इम्यूलेट किया गया हो या उसे हटाया जा सकता हो. भरोसेमंद वॉल्यूम वह होता है जिसे ऐप्लिकेशन ज़्यादातर समय ऐक्सेस कर सकता है.
Download
डायरेक्ट्री.
इसके अलावा, Android 11 (एपीआई लेवल 30) और इसके बाद के वर्शन पर, ACTION_OPEN_DOCUMENT_TREE
इंटेंट ऐक्शन का इस्तेमाल करके, उपयोगकर्ता से इन डायरेक्ट्री से अलग-अलग फ़ाइलें चुनने का अनुरोध नहीं किया जा सकता:
Android/data/
डायरेक्ट्री और सभी सबडायरेक्ट्री.Android/obb/
डायरेक्ट्री और सभी सबडायरेक्ट्री.
चुनी गई जगह पर कार्रवाइयां करना
जब कोई व्यक्ति सिस्टम के फ़ाइल पिकर का इस्तेमाल करके कोई फ़ाइल या डायरेक्ट्री चुन लेता है, तब onActivityResult()
में मौजूद इस कोड का इस्तेमाल करके, चुने गए आइटम का यूआरआई वापस पाया जा सकता है:
Kotlin
override fun onActivityResult( requestCode: Int, resultCode: Int, resultData: Intent?) { if (requestCode == your-request-code && resultCode == Activity.RESULT_OK) { // The result data contains a URI for the document or directory that // the user selected. resultData?.data?.also { uri -> // Perform operations on the document using its URI. } } }
Java
@Override public void onActivityResult(int requestCode, int resultCode, Intent resultData) { if (requestCode == your-request-code && resultCode == Activity.RESULT_OK) { // The result data contains a URI for the document or directory that // the user selected. Uri uri = null; if (resultData != null) { uri = resultData.getData(); // Perform operations on the document using its URI. } } }
चुने गए आइटम के यूआरआई का रेफ़रंस पाकर, आपका ऐप्लिकेशन उस आइटम पर कई कार्रवाइयां कर सकता है. उदाहरण के लिए, आइटम के मेटाडेटा को ऐक्सेस किया जा सकता है, आइटम में बदलाव किया जा सकता है, और आइटम को मिटाया जा सकता है.
यहां दिए गए सेक्शन में, उन फ़ाइलों पर कार्रवाइयां पूरी करने का तरीका बताया गया है जिन्हें उपयोगकर्ता चुनता है.
यह तय करना कि सेवा देने वाली कंपनी कौनसी कार्रवाइयां कर सकती है
कॉन्टेंट उपलब्ध कराने वाली अलग-अलग कंपनियां, दस्तावेज़ों पर अलग-अलग कार्रवाइयां करने की अनुमति देती हैं. जैसे, दस्तावेज़ कॉपी करना या दस्तावेज़ का थंबनेल देखना. यह पता लगाने के लिए कि कोई प्रोवाइडर कौनसी कार्रवाइयां कर सकता है, Document.COLUMN_FLAGS
की वैल्यू देखें.
इसके बाद, आपके ऐप्लिकेशन का यूज़र इंटरफ़ेस (यूआई) सिर्फ़ उन विकल्पों को दिखा सकता है जिन्हें सेवा देने वाली कंपनी सपोर्ट करती है.
अनुमतियां सेव करना
जब आपका ऐप्लिकेशन किसी फ़ाइल को पढ़ने या उसमें बदलाव करने के लिए खोलता है, तो सिस्टम आपके ऐप्लिकेशन को उस फ़ाइल के लिए यूआरआई की अनुमति देता है. यह अनुमति तब तक मान्य रहती है, जब तक उपयोगकर्ता का डिवाइस रीस्टार्ट नहीं हो जाता. मान लें कि आपका ऐप्लिकेशन, इमेज में बदलाव करने वाला ऐप्लिकेशन है. आपको उपयोगकर्ताओं को उन पांच इमेज का ऐक्सेस देना है जिनमें उन्होंने हाल ही में बदलाव किया है. इसके लिए, उन्हें सीधे आपके ऐप्लिकेशन से इमेज ऐक्सेस करने की सुविधा देनी है. अगर उपयोगकर्ता का डिवाइस रीस्टार्ट हो जाता है, तो आपको उसे फ़ाइलें ढूंढने के लिए, सिस्टम पिकर पर वापस भेजना होगा.
डिवाइस को रीस्टार्ट करने के बाद भी फ़ाइलों को ऐक्सेस करने की अनुमति बनाए रखने और उपयोगकर्ताओं को बेहतर अनुभव देने के लिए, आपका ऐप्लिकेशन सिस्टम की ओर से दी गई, यूआरआई को ऐक्सेस करने की अनुमति "ले" सकता है. इसे नीचे दिए गए कोड स्निपेट में दिखाया गया है:
Kotlin
val contentResolver = applicationContext.contentResolver val takeFlags: Int = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION // Check for the freshest data. contentResolver.takePersistableUriPermission(uri, takeFlags)
Java
final int takeFlags = intent.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); // Check for the freshest data. getContentResolver().takePersistableUriPermission(uri, takeFlags);
दस्तावेज़ के मेटाडेटा की जांच करना
किसी दस्तावेज़ का यूआरआई होने पर, आपको उसके मेटाडेटा का ऐक्सेस मिल जाता है. यह स्निपेट, यूआरआई से तय किए गए दस्तावेज़ के मेटाडेटा को इकट्ठा करता है और उसे लॉग करता है:
Kotlin
val contentResolver = applicationContext.contentResolver fun dumpImageMetaData(uri: Uri) { // The query, because it only applies to a single document, returns only // one row. There's no need to filter, sort, or select fields, // because we want all fields for one document. val cursor: Cursor? = contentResolver.query( uri, null, null, null, null, null) cursor?.use { // moveToFirst() returns false if the cursor has 0 rows. Very handy for // "if there's anything to look at, look at it" conditionals. if (it.moveToFirst()) { // Note it's called "Display Name". This is // provider-specific, and might not necessarily be the file name. val displayName: String = it.getString(it.getColumnIndex(OpenableColumns.DISPLAY_NAME)) Log.i(TAG, "Display Name: $displayName") val sizeIndex: Int = it.getColumnIndex(OpenableColumns.SIZE) // If the size is unknown, the value stored is null. But because an // int can't be null, the behavior is implementation-specific, // and unpredictable. So as // a rule, check if it's null before assigning to an int. This will // happen often: The storage API allows for remote files, whose // size might not be locally known. val size: String = if (!it.isNull(sizeIndex)) { // Technically the column stores an int, but cursor.getString() // will do the conversion automatically. it.getString(sizeIndex) } else { "Unknown" } Log.i(TAG, "Size: $size") } } }
Java
public void dumpImageMetaData(Uri uri) { // The query, because it only applies to a single document, returns only // one row. There's no need to filter, sort, or select fields, // because we want all fields for one document. Cursor cursor = getActivity().getContentResolver() .query(uri, null, null, null, null, null); try { // moveToFirst() returns false if the cursor has 0 rows. Very handy for // "if there's anything to look at, look at it" conditionals. if (cursor != null && cursor.moveToFirst()) { // Note it's called "Display Name". This is // provider-specific, and might not necessarily be the file name. String displayName = cursor.getString( cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); Log.i(TAG, "Display Name: " + displayName); int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE); // If the size is unknown, the value stored is null. But because an // int can't be null, the behavior is implementation-specific, // and unpredictable. So as // a rule, check if it's null before assigning to an int. This will // happen often: The storage API allows for remote files, whose // size might not be locally known. String size = null; if (!cursor.isNull(sizeIndex)) { // Technically the column stores an int, but cursor.getString() // will do the conversion automatically. size = cursor.getString(sizeIndex); } else { size = "Unknown"; } Log.i(TAG, "Size: " + size); } } finally { cursor.close(); } }
कोई दस्तावेज़ खोलना
किसी दस्तावेज़ के यूआरआई का रेफ़रंस होने पर, उसे आगे की प्रोसेसिंग के लिए खोला जा सकता है. इस सेक्शन में, बिटमैप और इनपुट स्ट्रीम खोलने के उदाहरण दिए गए हैं.
बिट मैप
नीचे दिए गए कोड स्निपेट में, किसी Bitmap
फ़ाइल को उसके यूआरआई से खोलने का तरीका बताया गया है:
Kotlin
val contentResolver = applicationContext.contentResolver @Throws(IOException::class) private fun getBitmapFromUri(uri: Uri): Bitmap { val parcelFileDescriptor: ParcelFileDescriptor = contentResolver.openFileDescriptor(uri, "r") val fileDescriptor: FileDescriptor = parcelFileDescriptor.fileDescriptor val image: Bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor) parcelFileDescriptor.close() return image }
Java
private Bitmap getBitmapFromUri(Uri uri) throws IOException { ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r"); FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor(); Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor); parcelFileDescriptor.close(); return image; }
बिटमैप खोलने के बाद, उसे ImageView
में दिखाया जा सकता है.
इनपुट स्ट्रीम
यहां दिए गए कोड स्निपेट में, किसी InputStream ऑब्जेक्ट को उसके यूआरआई से खोलने का तरीका बताया गया है. इस स्निपेट में, फ़ाइल की लाइनों को स्ट्रिंग में पढ़ा जा रहा है:
Kotlin
val contentResolver = applicationContext.contentResolver @Throws(IOException::class) private fun readTextFromUri(uri: Uri): String { val stringBuilder = StringBuilder() contentResolver.openInputStream(uri)?.use { inputStream -> BufferedReader(InputStreamReader(inputStream)).use { reader -> var line: String? = reader.readLine() while (line != null) { stringBuilder.append(line) line = reader.readLine() } } } return stringBuilder.toString() }
Java
private String readTextFromUri(Uri uri) throws IOException { StringBuilder stringBuilder = new StringBuilder(); try (InputStream inputStream = getContentResolver().openInputStream(uri); BufferedReader reader = new BufferedReader( new InputStreamReader(Objects.requireNonNull(inputStream)))) { String line; while ((line = reader.readLine()) != null) { stringBuilder.append(line); } } return stringBuilder.toString(); }
किसी दस्तावेज़ में बदलाव करना
टेक्स्ट दस्तावेज़ में बदलाव करने के लिए, स्टोरेज ऐक्सेस फ़्रेमवर्क का इस्तेमाल किया जा सकता है.
नीचे दिया गया कोड स्निपेट, दिए गए यूआरआई से दिखाए गए दस्तावेज़ के कॉन्टेंट को बदल देता है:
Kotlin
val contentResolver = applicationContext.contentResolver private fun alterDocument(uri: Uri) { try { contentResolver.openFileDescriptor(uri, "w")?.use { FileOutputStream(it.fileDescriptor).use { it.write( ("Overwritten at ${System.currentTimeMillis()}\n") .toByteArray() ) } } } catch (e: FileNotFoundException) { e.printStackTrace() } catch (e: IOException) { e.printStackTrace() } }
Java
private void alterDocument(Uri uri) { try { ParcelFileDescriptor pfd = getActivity().getContentResolver(). openFileDescriptor(uri, "w"); FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor()); fileOutputStream.write(("Overwritten at " + System.currentTimeMillis() + "\n").getBytes()); // Let the document provider know you're done by closing the stream. fileOutputStream.close(); pfd.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
किसी दस्तावेज़ को मिटाना
अगर आपके पास किसी दस्तावेज़ का यूआरआई है और दस्तावेज़ के Document.COLUMN_FLAGS
में SUPPORTS_DELETE
मौजूद है, तो दस्तावेज़ को मिटाया जा सकता है. उदाहरण के लिए:
Kotlin
DocumentsContract.deleteDocument(applicationContext.contentResolver, uri)
Java
DocumentsContract.deleteDocument(applicationContext.contentResolver, uri);
मिलता-जुलता मीडिया यूआरआई वापस पाएं
getMediaUri()
मेथड, मीडिया स्टोर का ऐसा यूआरआई उपलब्ध कराता है जो दिए गए दस्तावेज़ों के
प्रोवाइडर यूआरआई के बराबर होता है. दोनों यूआरआई, एक ही आइटम को दिखाते हैं. मीडिया स्टोर यूआरआई का इस्तेमाल करके, शेयर किए गए स्टोरेज से मीडिया फ़ाइलों को आसानी से ऐक्सेस किया जा सकता है.
getMediaUri()
मेथड, ExternalStorageProvider
यूआरआई के साथ काम करता है. Android 12 (एपीआई लेवल 31) और इसके बाद के वर्शन पर, यह तरीका MediaDocumentsProvider
यूआरआई के साथ भी काम करता है.
वर्चुअल फ़ाइल खोलना
Android 7.0 (एपीआई लेवल 25) और इसके बाद के वर्शन पर, आपका ऐप्लिकेशन उन वर्चुअल फ़ाइलों का इस्तेमाल कर सकता है जिन्हें Storage Access Framework उपलब्ध कराता है. वर्चुअल फ़ाइलों का बाइनरी फ़ॉर्मैट नहीं होता. हालांकि, आपका ऐप्लिकेशन उनके कॉन्टेंट को खोल सकता है. इसके लिए, उन्हें किसी दूसरे फ़ाइल टाइप में बदला जा सकता है या ACTION_VIEW
इंटेंट ऐक्शन का इस्तेमाल करके उन फ़ाइलों को देखा जा सकता है.
वर्चुअल फ़ाइलें खोलने के लिए, आपके क्लाइंट ऐप्लिकेशन में उन्हें हैंडल करने के लिए खास लॉजिक शामिल होना चाहिए. अगर आपको फ़ाइल का बाइट फ़ॉर्मैट चाहिए, तो आपको दस्तावेज़ उपलब्ध कराने वाली कंपनी से किसी दूसरे MIME टाइप का अनुरोध करना होगा. उदाहरण के लिए, फ़ाइल की झलक देखने के लिए.
उपयोगकर्ता के किसी विकल्प को चुनने के बाद, नतीजों के डेटा में मौजूद यूआरआई का इस्तेमाल करके यह पता लगाएं कि फ़ाइल वर्चुअल है या नहीं. इसके लिए, यहां दिए गए कोड स्निपेट का इस्तेमाल करें:
Kotlin
private fun isVirtualFile(uri: Uri): Boolean { if (!DocumentsContract.isDocumentUri(this, uri)) { return false } val cursor: Cursor? = contentResolver.query( uri, arrayOf(DocumentsContract.Document.COLUMN_FLAGS), null, null, null ) val flags: Int = cursor?.use { if (cursor.moveToFirst()) { cursor.getInt(0) } else { 0 } } ?: 0 return flags and DocumentsContract.Document.FLAG_VIRTUAL_DOCUMENT != 0 }
Java
private boolean isVirtualFile(Uri uri) { if (!DocumentsContract.isDocumentUri(this, uri)) { return false; } Cursor cursor = getContentResolver().query( uri, new String[] { DocumentsContract.Document.COLUMN_FLAGS }, null, null, null); int flags = 0; if (cursor.moveToFirst()) { flags = cursor.getInt(0); } cursor.close(); return (flags & DocumentsContract.Document.FLAG_VIRTUAL_DOCUMENT) != 0; }
दस्तावेज़ के वर्चुअल फ़ाइल होने की पुष्टि करने के बाद, फ़ाइल को किसी दूसरे MIME टाइप में बदला जा सकता है. जैसे, "image/png"
. यहां दिए गए कोड स्निपेट में बताया गया है कि किसी वर्चुअल फ़ाइल को इमेज के तौर पर दिखाया जा सकता है या नहीं. अगर ऐसा किया जा सकता है, तो वर्चुअल फ़ाइल से इनपुट स्ट्रीम मिलती है:
Kotlin
@Throws(IOException::class) private fun getInputStreamForVirtualFile( uri: Uri, mimeTypeFilter: String): InputStream { val openableMimeTypes: Array<String>? = contentResolver.getStreamTypes(uri, mimeTypeFilter) return if (openableMimeTypes?.isNotEmpty() == true) { contentResolver .openTypedAssetFileDescriptor(uri, openableMimeTypes[0], null) .createInputStream() } else { throw FileNotFoundException() } }
Java
private InputStream getInputStreamForVirtualFile(Uri uri, String mimeTypeFilter) throws IOException { ContentResolver resolver = getContentResolver(); String[] openableMimeTypes = resolver.getStreamTypes(uri, mimeTypeFilter); if (openableMimeTypes == null || openableMimeTypes.length < 1) { throw new FileNotFoundException(); } return resolver .openTypedAssetFileDescriptor(uri, openableMimeTypes[0], null) .createInputStream(); }
अन्य संसाधन
दस्तावेज़ों और अन्य फ़ाइलों को सेव करने और उन्हें ऐक्सेस करने के तरीके के बारे में ज़्यादा जानने के लिए, यहां दिए गए संसाधन देखें.
सैंपल
- ActionOpenDocument, जो GitHub पर उपलब्ध है.
- ActionOpenDocumentTree, GitHub पर उपलब्ध है.