ফাইলের তথ্য ফিরিয়ে আনা হচ্ছে

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

এই টিউটোরিয়ালে দেখানো হয়েছে যে কীভাবে সার্ভার অ্যাপের FileProvider কোয়েরি করে ফাইলের MIME-এর প্রকার ও সাইজ সংক্রান্ত তথ্য পাওয়া যায়।

ফাইলের MIME-এর ধরন ফিরিয়ে আনুন

ফাইলের ডেটার ধরন ক্লায়েন্ট অ্যাপকে নির্দেশ করে যে ফাইলের কন্টেন্ট কীভাবে ম্যানেজ করতে হবে। কন্টেন্ট URI থেকে শেয়ার করা ফাইলের ডেটার ধরন জানতে, ক্লায়েন্ট অ্যাপ ContentResolver.getType()-কে কল করে। এই পদ্ধতি ফাইলের MIME-এর প্রকার রিটার্ন করে। ডিফল্ট হিসেবে, FileProvider ফাইলের MIME-এর প্রকার নির্ধারণ করে তার ফাইলের নামের এক্সটেনশন থেকে।

সার্ভার অ্যাপ ক্লায়েন্টকে কন্টেন্ট URI ফেরত দেওয়ার পরে, ক্লায়েন্ট অ্যাপ কীভাবে ফাইলের MIME-এর প্রকার একবার উদ্ধার করে তা নিম্নলিখিত কোড স্নিপেট থেকে বোঝা যায়:

Kotlin

    ...
    /*
     * Get the file's content URI from the incoming Intent, then
     * get the file's MIME type
     */
    val mimeType: String? = returnIntent.data?.let { returnUri ->
        contentResolver.getType(returnUri)
    }
    ...

জাভা

    ...
    /*
     * Get the file's content URI from the incoming Intent, then
     * get the file's MIME type
     */
    Uri returnUri = returnIntent.getData();
    String mimeType = getContentResolver().getType(returnUri);
    ...

ফাইলের নাম ও সাইজ ফিরিয়ে আনা

FileProvider ক্লাসে query() পদ্ধতির ডিফল্ট ইমপ্লিমেন্টেশন আছে যা Cursor-এ কন্টেন্ট URI-এর সাথে যুক্ত ফাইলের নাম ও সাইজ রিটার্ন করে। ডিফল্ট ইমপ্লিমেন্টেশন দুটি কলাম দেখায়:

DISPLAY_NAME
String হিসেবে ফাইলের নাম। এই ভ্যালু File.getName()-এর মাধ্যমে ফেরত দেওয়া ভ্যালুর সমান।
SIZE
বাইট হিসেবে ফাইলের সাইজ, long হিসেবে এই ভ্যালু File.length()-এর মাধ্যমে রিটার্ন করা ভ্যালুর মতোই

ক্লায়েন্ট অ্যাপ, কন্টেন্ট URI ছাড়া query()-এর সব আর্গুমেন্ট null-এ সেট করে, কোনও ফাইলের জন্য DISPLAY_NAME এবং SIZE, দুটিই পেতে পারে। যেমন, এই কোড স্নিপেট ফাইলের DISPLAY_NAME এবং SIZE ফিরিয়ে আনে এবং প্রতিটি আলাদা TextView-এ দেখায়:

Kotlin

    /*
     * Get the file's content URI from the incoming Intent,
     * then query the server app to get the file's display name
     * and size.
     */
    returnIntent.data?.let { returnUri ->
        contentResolver.query(returnUri, null, null, null, null)
    }?.use { cursor ->
        /*
         * Get the column indexes of the data in the Cursor,
         * move to the first row in the Cursor, get the data,
         * and display it.
         */
        val nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
        val sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE)
        cursor.moveToFirst()
        findViewById<TextView>(R.id.filename_text).text = cursor.getString(nameIndex)
        findViewById<TextView>(R.id.filesize_text).text = cursor.getLong(sizeIndex).toString()
        ...
    }

জাভা

    ...
    /*
     * Get the file's content URI from the incoming Intent,
     * then query the server app to get the file's display name
     * and size.
     */
    Uri returnUri = returnIntent.getData();
    Cursor returnCursor =
            getContentResolver().query(returnUri, null, null, null, null);
    /*
     * Get the column indexes of the data in the Cursor,
     * move to the first row in the Cursor, get the data,
     * and display it.
     */
    int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
    int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
    returnCursor.moveToFirst();
    TextView nameView = (TextView) findViewById(R.id.filename_text);
    TextView sizeView = (TextView) findViewById(R.id.filesize_text);
    nameView.setText(returnCursor.getString(nameIndex));
    sizeView.setText(Long.toString(returnCursor.getLong(sizeIndex)));
    ...

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