शेयर की गई फ़ाइल का अनुरोध करना

जब कोई ऐप्लिकेशन, किसी अन्य ऐप्लिकेशन के ज़रिए शेयर की गई फ़ाइल को ऐक्सेस करना चाहता है, तो अनुरोध करने वाला ऐप्लिकेशन (क्लाइंट) आम तौर पर, फ़ाइलें शेयर करने वाले ऐप्लिकेशन को अनुरोध भेजता है (सर्वर). ज़्यादातर मामलों में, अनुरोध सर्वर ऐप्लिकेशन में Activity शुरू करता है, जो उन फ़ाइलों को दिखाता है जिन्हें वह शेयर कर सकता है. उपयोगकर्ता एक फ़ाइल चुनता है, जिसके बाद सर्वर ऐप्लिकेशन फ़ाइल का कॉन्टेंट यूआरआई क्लाइंट ऐप्लिकेशन में मिलेगा.

इस लेसन में बताया गया है कि क्लाइंट ऐप्लिकेशन, सर्वर ऐप्लिकेशन से फ़ाइल का अनुरोध कैसे करता है और फ़ाइल की कॉन्टेंट यूआरआई को अपलोड करता है और कॉन्टेंट यूआरआई का इस्तेमाल करके फ़ाइल खोलता है.

फ़ाइल के लिए अनुरोध भेजें

सर्वर ऐप्लिकेशन से किसी फ़ाइल का अनुरोध करने के लिए, क्लाइंट ऐप्लिकेशन startActivityForResult Intent में यह कार्रवाई शामिल है ACTION_PICK और MIME टाइप जिसे क्लाइंट ऐप्लिकेशन के लिए इस्तेमाल किया जाता है संभाल सकता है.

उदाहरण के लिए, नीचे दिया गया कोड स्निपेट किसी सर्वर ऐप्लिकेशन को Intent फ़ाइल शेयर करना में बताए गए Activity:

Kotlin

class MainActivity : Activity() {
    private lateinit var requestFileIntent: Intent
    private lateinit var inputPFD: ParcelFileDescriptor
    ...
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        requestFileIntent = Intent(Intent.ACTION_PICK).apply {
            type = "image/jpg"
        }
        ...
    }
    ...
    private fun requestFile() {
        /**
         * When the user requests a file, send an Intent to the
         * server app.
         * files.
         */
        startActivityForResult(requestFileIntent, 0)
        ...
    }
    ...
}

Java

public class MainActivity extends Activity {
    private Intent requestFileIntent;
    private ParcelFileDescriptor inputPFD;
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        requestFileIntent = new Intent(Intent.ACTION_PICK);
        requestFileIntent.setType("image/jpg");
        ...
    }
    ...
    protected void requestFile() {
        /**
         * When the user requests a file, send an Intent to the
         * server app.
         * files.
         */
            startActivityForResult(requestFileIntent, 0);
        ...
    }
    ...
}

जिस फ़ाइल के लिए अनुरोध किया गया है उसे ऐक्सेस करें

सर्वर ऐप्लिकेशन फ़ाइल के कॉन्टेंट यूआरआई को Intent. यह Intent, क्लाइंट को भेज दिया गया है ऐप्लिकेशन को onActivityResult() में बदलना ज़रूरी है. एक बार क्लाइंट ऐप्लिकेशन में फ़ाइल का कॉन्टेंट यूआरआई है, तो वह FileDescriptor.

इस प्रोसेस में, फ़ाइल की सुरक्षा सिर्फ़ तब तक सुरक्षित रखी जाती है, जब तक कॉन्टेंट यूआरआई को सही तरीके से पार्स किया जाता है जो क्लाइंट ऐप्लिकेशन को मिलते हैं. कॉन्टेंट पार्स करते समय, आपको पक्का करना होगा कि यह यूआरआई की अनुमति के बिना किसी भी नीति का पालन करना चाहिए, ताकि यह पक्का किया जा सके कि पाथ ट्रैवर्सल की कोशिश की जा रही है. फ़ाइल का ऐक्सेस सिर्फ़ क्लाइंट ऐप्लिकेशन को मिलना चाहिए. साथ ही, फ़ाइल ऐक्सेस करने के लिए, सर्वर ऐप्लिकेशन के साथ काम करता है. अनुमतियां कुछ समय के लिए होती हैं. इसलिए, क्लाइंट ऐप्लिकेशन का टास्क स्टैक पूरा हो जाने पर, फ़ाइल को अब सर्वर ऐप्लिकेशन से बाहर ऐक्सेस नहीं किया जा सकता.

अगला स्निपेट दिखाता है कि क्लाइंट ऐप्लिकेशन Intent को सर्वर ऐप्लिकेशन से भेजा जाता है. साथ ही, यह भी पता चलता है कि क्लाइंट ऐप्लिकेशन को यह कैसे मिलता है कॉन्टेंट यूआरआई का इस्तेमाल करने वाला FileDescriptor:

Kotlin

/*
 * When the Activity of the app that hosts files sets a result and calls
 * finish(), this method is invoked. The returned Intent contains the
 * content URI of a selected file. The result code indicates if the
 * selection worked or not.
 */
public override fun onActivityResult(requestCode: Int, resultCode: Int, returnIntent: Intent) {
    // If the selection didn't work
    if (resultCode != Activity.RESULT_OK) {
        // Exit without doing anything else
        return
    }
    // Get the file's content URI from the incoming Intent
    returnIntent.data?.also { returnUri ->
        /*
         * Try to open the file for "read" access using the
         * returned URI. If the file isn't found, write to the
         * error log and return.
         */
        inputPFD = try {
            /*
             * Get the content resolver instance for this context, and use it
             * to get a ParcelFileDescriptor for the file.
             */
            contentResolver.openFileDescriptor(returnUri, "r")
        } catch (e: FileNotFoundException) {
            e.printStackTrace()
            Log.e("MainActivity", "File not found.")
            return
        }

        // Get a regular file descriptor for the file
        val fd = inputPFD.fileDescriptor
        ...
    }
}

Java

    /*
     * When the Activity of the app that hosts files sets a result and calls
     * finish(), this method is invoked. The returned Intent contains the
     * content URI of a selected file. The result code indicates if the
     * selection worked or not.
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode,
            Intent returnIntent) {
        // If the selection didn't work
        if (resultCode != RESULT_OK) {
            // Exit without doing anything else
            return;
        } else {
            // Get the file's content URI from the incoming Intent
            Uri returnUri = returnIntent.getData();
            /*
             * Try to open the file for "read" access using the
             * returned URI. If the file isn't found, write to the
             * error log and return.
             */
            try {
                /*
                 * Get the content resolver instance for this context, and use it
                 * to get a ParcelFileDescriptor for the file.
                 */
                inputPFD = getContentResolver().openFileDescriptor(returnUri, "r");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Log.e("MainActivity", "File not found.");
                return;
            }
            // Get a regular file descriptor for the file
            FileDescriptor fd = inputPFD.getFileDescriptor();
            ...
        }
    }

तरीका openFileDescriptor() फ़ाइल के लिए ParcelFileDescriptor दिखाता है. इस ऑब्जेक्ट से, क्लाइंट ऐप्लिकेशन को एक FileDescriptor ऑब्जेक्ट मिलता है, जिसका इस्तेमाल वह फ़ाइल को पढ़ने के लिए कर सकता है.

इससे जुड़ी अतिरिक्त जानकारी के लिए, इन्हें देखें: