การดึงข้อมูลไฟล์
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ก่อนที่แอปไคลเอ็นต์จะพยายามทำงานกับไฟล์ที่มี URI เนื้อหา แอปสามารถ
ขอข้อมูลเกี่ยวกับไฟล์จากแอปเซิร์ฟเวอร์ รวมถึงประเภทข้อมูลของไฟล์และ
ขนาดไฟล์ ประเภทข้อมูลจะช่วยให้แอปไคลเอ็นต์พิจารณาว่าจะจัดการกับไฟล์ได้หรือไม่ และ
ขนาดไฟล์จะช่วยให้แอปไคลเอ็นต์ตั้งค่าการบัฟเฟอร์และแคชไฟล์ได้
บทเรียนนี้จะสาธิตวิธีค้นหาแอปของเซิร์ฟเวอร์
FileProvider
เพื่อเรียกข้อมูลประเภทและขนาด MIME ของไฟล์
ดึงข้อมูลประเภท MIME ของไฟล์
ประเภทข้อมูลของไฟล์จะเป็นตัวบอกให้แอปไคลเอ็นต์ทราบว่าไฟล์ควรจัดการกับเนื้อหาของไฟล์อย่างไร โดยวิธีการมีดังนี้
ประเภทข้อมูลของไฟล์ที่แชร์ตาม URI เนื้อหา การเรียกใช้แอปไคลเอ็นต์
ContentResolver.getType()
เมธอดนี้จะส่งคืน
ประเภท MIME ของไฟล์ โดยค่าเริ่มต้น แอตทริบิวต์
FileProvider
ระบุประเภท MIME ของไฟล์จาก
นามสกุลไฟล์
ข้อมูลโค้ดต่อไปนี้จะแสดงให้เห็นว่าแอปไคลเอ็นต์เรียกไฟล์ประเภท MIME เพียงครั้งเดียว
แอปเซิร์ฟเวอร์แสดงผล URI เนื้อหาไปยังไคลเอ็นต์แล้ว
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)
}
...
Java
...
/*
* 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()
ที่แสดงผล
ชื่อและขนาดของไฟล์ที่เชื่อมโยงกับ URI เนื้อหาใน
Cursor
การใช้งานเริ่มต้นจะแสดง 2 คอลัมน์ดังนี้
DISPLAY_NAME
-
ชื่อไฟล์ที่เป็น
String
ค่านี้เหมือนกับค่าที่แสดงผล
โดย File.getName()
SIZE
-
ขนาดของไฟล์ในหน่วยไบต์ ในรูปแบบ
long
ค่านี้จะเหมือนกับค่า
File.length()
ส่งคืน
แอปไคลเอ็นต์สามารถรับทั้ง DISPLAY_NAME
และ SIZE
สำหรับไฟล์โดยการตั้งค่าทั้งหมด
ของอาร์กิวเมนต์ของ query()
ไปยัง
null
ยกเว้น URI เนื้อหา ตัวอย่างเช่น ข้อมูลโค้ดนี้จะดึงส่วน
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()
...
}
Java
...
/*
* 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)));
...
ดูข้อมูลที่เกี่ยวข้องเพิ่มเติมได้ที่
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 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-27 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)"]]