파일 정보 가져오기
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
클라이언트 앱이 콘텐츠 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)
}
...
자바
...
/*
* 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
입니다. 기본 구현에서는 다음과 같은 두 개의 열을 반환합니다.
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()
...
}
자바
...
/*
* 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)));
...
추가 관련 정보는 다음을 참조하세요.
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 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)"]]