বাংলায় অ্যান্ড্রয়েড সহায়িকা - Bangla Android Guide

রিকোয়েস্ট করা ফাইলে প্রবেশ

সারভার অ্যাপ ফাইলের কনটেন্ট ইউআরআই একটি Intent এর মধ্যে ক্লায়েন্ট অ্যাপে ফেরত পাঠায়। এই Intent ক্লায়েন্ট অ্যাপে এটার onActivityResult()এর ওভাররাইডে পাস হয়। কখনও ক্লায়েন্ট অ্যাপের ফাইলের কনটেন্ট ইউআরআই থাকে, এটা ফাইলে প্রবেশ করতে পারে এটার FileDescriptor পাওয়ার মাধ্যমে।

ফাইল নিরাপত্তা এই পক্রিয়ার মধ্যে সংরক্ষিত আছে কারন কনটেন্ট ইউআরআই হচ্ছে ডাটার একমাত্র অংশ যা ক্লায়েন্ট অ্যাপ গ্রহণ করে। যেহেতু এই ইউআরআই একটি ডিরেক্টরী পাথ ধারন না করে, ক্লায়েন্ট অ্যাপ সারভার অ্যাপের অন্য ফাইল আবিস্কার এবং ওপেন করতে পারে না। শুধুমাত্র ক্লায়েন্ট অ্যাপ ফাইলে প্রবেশগম্যতা লাভ করে, এবং শুধুমাত্র সারভার অ্যাপ কর্তৃক পারমিশন প্রদান করা হয়। পারমিশনটা অস্থায়ী, তাই একসময় ক্লায়েন্ট অ্যাপের কার্যক্রম শেষ হলে, সারভার অ্যাপের বাইরে থেকে ফাইলের প্রবেশগম্যতা বিদ্যমান থাকে না। নি¤েœাক্ত অংশটি দেখায় সারভার অ্যাপ থেকে পাঠানো Intent কে ক্লায়েন্ট অ্যাপ কীভাবে চালিত করে, এবং ইউআরআই ব্যবহার করে কীভাবে ক্লায়েন্ট অ্যাপ 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.
                 */
                mInputPFD = 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 = mInputPFD.getFileDescriptor();
            ...
        }
    }

মেথড openFileDescrioptor()ফাইলের জন্য একটি ParcelFileDescriptor ফেরত পাঠায়। এই অবজেক্ট থেকে, ক্লায়েন্ট অ্যাপ একটি FileDescriptor অবজেক্ট লাভ করে, এটা পরবর্তীতে ফাইল রিড করতে ব্যবহৃত হয়।