শেয়ার করা ফাইলের জন্য অনুরোধ করা হচ্ছে

কোনও অ্যাপ অন্য অ্যাপের শেয়ার করা ফাইল অ্যাক্সেস করতে চাইলে, অনুরোধকারী অ্যাপ (ক্লায়েন্ট) সাধারণত ফাইল শেয়ার করা অ্যাপকে (সার্ভার) অনুরোধ পাঠায়। বেশিরভাগ ক্ষেত্রে, অনুরোধটি সার্ভার অ্যাপে একটি Activity শুরু করে যা শেয়ার করতে পারে এমন ফাইলগুলি দেখায়। ব্যবহারকারী একটি ফাইল বেছে নেন, যার পরে সার্ভার অ্যাপটি ক্লায়েন্ট অ্যাপে ফাইলের কন্টেন্ট URI ফেরত দেয়।

এই টিউটোরিয়াল থেকে আপনি জানতে পারবেন যে কীভাবে ক্লায়েন্ট অ্যাপ সার্ভার অ্যাপ থেকে ফাইলের জন্য অনুরোধ করে, সার্ভার অ্যাপ থেকে ফাইলের কন্টেন্ট URI পায় এবং কন্টেন্ট URI ব্যবহার করে ফাইল খোলে।

ফাইলের জন্য অনুরোধ পাঠান

সার্ভার অ্যাপ থেকে ফাইলের অনুরোধ করতে, ক্লায়েন্ট অ্যাপ startActivityForResult-কে কল করে Intent-এর সাথে, যার মধ্যে ACTION_PICK-এর মতো অ্যাকশন এবং MIME-এর প্রকার থাকে যা ক্লায়েন্ট অ্যাপ ম্যানেজ করতে পারে।

যেমন, নিম্নলিখিত কোড স্নিপেট থেকে বোঝা যায় যে ফাইল শেয়ার করা বিভাগে বর্ণিত Activity শুরু করার জন্য সার্ভার অ্যাপে কীভাবে Intent পাঠাতে হয়:

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)
        ...
    }
    ...
}

জাভা

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);
        ...
    }
    ...
}

অনুরোধ করা ফাইল অ্যাক্সেস করুন

সার্ভার অ্যাপ, ক্লায়েন্ট অ্যাপে ফাইলের কন্টেন্ট URI, Intent-এ ফেরত পাঠায়। এই Intent ক্লায়েন্ট অ্যাপে পাস করা হয় যা onActivityResult() ওভাররাইড করে। ক্লায়েন্ট অ্যাপের কাছে ফাইলের কন্টেন্ট URI থাকলে, এটি FileDescriptor পেয়ে ফাইলটি অ্যাক্সেস করতে পারে।

এই প্রসেসে ফাইলের নিরাপত্তা তখনই বজায় থাকে যখন আপনি ক্লায়েন্ট অ্যাপের পাওয়া কন্টেন্ট URI সঠিকভাবে পার্স করেন। কন্টেন্ট পার্স করার সময়, আপনাকে নিশ্চিত করতে হবে যে এই URI যেন উদ্দিষ্ট ডিরেক্টরির বাইরে কোনও কিছুকে পয়েন্ট না করে, যাতে কোনও পাথ ট্র্যাভারসাল-এর চেষ্টা না করা হয়। শুধুমাত্র ক্লায়েন্ট অ্যাপের ফাইলের অ্যাক্সেস পাওয়া উচিত এবং শুধুমাত্র সার্ভার অ্যাপের মাধ্যমে মঞ্জুর করা অনুমতির জন্য। অনুমতিগুলি অস্থায়ী, তাই ক্লায়েন্ট অ্যাপের টাস্ক স্ট্যাক শেষ হয়ে গেলে, ফাইলটি সার্ভার অ্যাপের বাইরে আর অ্যাক্সেসযোগ্য থাকে না।

পরবর্তী স্নিপেট দেখায় যে ক্লায়েন্ট অ্যাপ কীভাবে সার্ভার অ্যাপ থেকে পাঠানো Intent ম্যানেজ করে এবং ক্লায়েন্ট অ্যাপ কীভাবে কন্টেন্ট URI ব্যবহার করে 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
        ...
    }
}

জাভা

    /*
     * 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 অবজেক্ট পায়, যা এটি ফাইলটি পড়ার জন্য ব্যবহার করতে পারে।

আরও সম্পর্কিত তথ্যের জন্য, এখানে দেখুন: