ফাইল তথ্য পুনরুদ্ধার করা হচ্ছে
সেভ করা পৃষ্ঠা গুছিয়ে রাখতে 'সংগ্রহ' ব্যবহার করুন
আপনার পছন্দ অনুযায়ী কন্টেন্ট সেভ করুন ও সঠিক বিভাগে রাখুন।
একটি ক্লায়েন্ট অ্যাপ এমন একটি ফাইলের সাথে কাজ করার চেষ্টা করার আগে যার জন্য এটির একটি বিষয়বস্তু URI আছে, অ্যাপটি সার্ভার অ্যাপ থেকে ফাইলটির ডেটা টাইপ এবং ফাইলের আকার সহ ফাইল সম্পর্কে তথ্যের অনুরোধ করতে পারে। ডেটা টাইপ ক্লায়েন্ট অ্যাপটিকে ফাইলটি পরিচালনা করতে পারে কিনা তা নির্ধারণ করতে সহায়তা করে এবং ফাইলের আকার ক্লায়েন্ট অ্যাপটিকে ফাইলের জন্য বাফারিং এবং ক্যাশিং সেট আপ করতে সহায়তা করে।
এই পাঠটি দেখায় কিভাবে একটি ফাইলের MIME প্রকার এবং আকার পুনরুদ্ধার করতে সার্ভার অ্যাপের FileProvider
জিজ্ঞাসা করতে হয়।
একটি ফাইলের MIME প্রকার পুনরুদ্ধার করুন৷
একটি ফাইলের ডেটা টাইপ ক্লায়েন্ট অ্যাপকে নির্দেশ করে যে এটি ফাইলের বিষয়বস্তু কীভাবে পরিচালনা করবে। একটি শেয়ার করা ফাইলের ডেটা টাইপ পেতে এর সামগ্রী URI দেওয়া হয়েছে, ক্লায়েন্ট অ্যাপটি ContentResolver.getType()
কল করে। এই পদ্ধতিটি ফাইলের MIME প্রকার প্রদান করে। ডিফল্টরূপে, একটি FileProvider
তার ফাইলের নাম এক্সটেনশন থেকে ফাইলের MIME প্রকার নির্ধারণ করে।
নিম্নলিখিত কোড স্নিপেটটি দেখায় যে কিভাবে একটি ক্লায়েন্ট অ্যাপ MIME ধরনের ফাইল পুনরুদ্ধার করে যখন সার্ভার অ্যাপটি ক্লায়েন্টকে কন্টেন্ট URI ফেরত দেয়:
কোটলিন
...
/*
* 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
প্রদর্শন করে:
কোটলিন
/*
* 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)));
...
অতিরিক্ত সম্পর্কিত তথ্যের জন্য, পড়ুন:
এই পৃষ্ঠার কন্টেন্ট ও কোডের নমুনাগুলি Content License-এ বর্ণিত লাইসেন্সের অধীনস্থ। Java এবং OpenJDK হল Oracle এবং/অথবা তার অ্যাফিলিয়েট সংস্থার রেজিস্টার্ড ট্রেডমার্ক।
2025-07-29 UTC-তে শেষবার আপডেট করা হয়েছে।
[[["সহজে বোঝা যায়","easyToUnderstand","thumb-up"],["আমার সমস্যার সমাধান হয়েছে","solvedMyProblem","thumb-up"],["অন্যান্য","otherUp","thumb-up"]],[["এতে আমার প্রয়োজনীয় তথ্য নেই","missingTheInformationINeed","thumb-down"],["খুব জটিল / অনেক ধাপ","tooComplicatedTooManySteps","thumb-down"],["পুরনো","outOfDate","thumb-down"],["অনুবাদ সংক্রান্ত সমস্যা","translationIssue","thumb-down"],["নমুনা / কোড সংক্রান্ত সমস্যা","samplesCodeIssue","thumb-down"],["অন্যান্য","otherDown","thumb-down"]],["2025-07-29 UTC-তে শেষবার আপডেট করা হয়েছে।"],[],[],null,["# Retrieving file information\n\nBefore a client app tries to work with a file for which it has a content URI, the app can\nrequest information about the file from the server app, including the file's data type and\nfile size. The data type helps the client app to determine if it can handle the file, and the\nfile size helps the client app set up buffering and caching for the file.\n\n\nThis lesson demonstrates how to query the server app's\n[FileProvider](/reference/androidx/core/content/FileProvider) to retrieve a file's MIME type and size.\n\nRetrieve a file's MIME type\n---------------------------\n\n\nA file's data type indicates to the client app how it should handle the file's contents. To get\nthe data type of a shared file given its content URI, the client app calls\n[ContentResolver.getType()](/reference/android/content/ContentResolver#getType(android.net.Uri)). This method returns\nthe file's MIME type. By default, a\n[FileProvider](/reference/androidx/core/content/FileProvider) determines the file's MIME type from its\nfilename extension.\n\n\nThe following code snippet demonstrates how a client app retrieves the MIME type of a file once\nthe server app has returned the content URI to the client: \n\n### Kotlin\n\n```kotlin\n ...\n /*\n * Get the file's content URI from the incoming Intent, then\n * get the file's MIME type\n */\n val mimeType: String? = returnIntent.data?.let { returnUri -\u003e\n contentResolver.getType(returnUri)\n }\n ...\n```\n\n### Java\n\n```java\n ...\n /*\n * Get the file's content URI from the incoming Intent, then\n * get the file's MIME type\n */\n Uri returnUri = returnIntent.getData();\n String mimeType = getContentResolver().getType(returnUri);\n ...\n```\n\nRetrieve a file's name and size\n-------------------------------\n\n\nThe [FileProvider](/reference/androidx/core/content/FileProvider) class has a default implementation of the\n[query()](/reference/android/content/ContentProvider#query(android.net.Uri, java.lang.String[], android.os.Bundle, android.os.CancellationSignal)) method that returns the\nname and size of the file associated with a content URI in a\n[Cursor](/reference/android/database/Cursor). The default implementation returns two columns:\n\n[DISPLAY_NAME](/reference/android/provider/OpenableColumns#DISPLAY_NAME)\n:\n The file's name, as a [String](/reference/java/lang/String). This value is the same as the value returned\n by [File.getName()](/reference/java/io/File#getName()).\n\n[SIZE](/reference/android/provider/OpenableColumns#SIZE)\n:\n The size of the file in bytes, as a `long` This value is the same as the value\n returned by [File.length()](/reference/java/io/File#length())\n\n\nThe client app can get both the [DISPLAY_NAME](/reference/android/provider/OpenableColumns#DISPLAY_NAME) and [SIZE](/reference/android/provider/OpenableColumns#SIZE) for a file by setting all\nof the arguments of [query()](/reference/android/content/ContentProvider#query(android.net.Uri, java.lang.String[], android.os.Bundle, android.os.CancellationSignal)) to\n`null` except for the content URI. For example, this code snippet retrieves a file's\n[DISPLAY_NAME](/reference/android/provider/OpenableColumns#DISPLAY_NAME) and\n[SIZE](/reference/android/provider/OpenableColumns#SIZE) and displays each one in separate\n[TextView](/reference/android/widget/TextView): \n\n### Kotlin\n\n```kotlin\n /*\n * Get the file's content URI from the incoming Intent,\n * then query the server app to get the file's display name\n * and size.\n */\n returnIntent.data?.let { returnUri -\u003e\n contentResolver.query(returnUri, null, null, null, null)\n }?.use { cursor -\u003e\n /*\n * Get the column indexes of the data in the Cursor,\n * move to the first row in the Cursor, get the data,\n * and display it.\n */\n val nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)\n val sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE)\n cursor.moveToFirst()\n findViewById\u003cTextView\u003e(R.id.filename_text).text = cursor.getString(nameIndex)\n findViewById\u003cTextView\u003e(R.id.filesize_text).text = cursor.getLong(sizeIndex).toString()\n ...\n }\n```\n\n### Java\n\n```java\n ...\n /*\n * Get the file's content URI from the incoming Intent,\n * then query the server app to get the file's display name\n * and size.\n */\n Uri returnUri = returnIntent.getData();\n Cursor returnCursor =\n getContentResolver().query(returnUri, null, null, null, null);\n /*\n * Get the column indexes of the data in the Cursor,\n * move to the first row in the Cursor, get the data,\n * and display it.\n */\n int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);\n int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);\n returnCursor.moveToFirst();\n TextView nameView = (TextView) findViewById(R.id.filename_text);\n TextView sizeView = (TextView) findViewById(R.id.filesize_text);\n nameView.setText(returnCursor.getString(nameIndex));\n sizeView.setText(Long.toString(returnCursor.getLong(sizeIndex)));\n ...\n```\n\nFor additional related information, refer to:\n\n- [Retrieving Data from the Provider](/guide/topics/providers/content-provider-basics#SimpleQuery)"]]