파일 정보 가져오기

클라이언트 앱이 콘텐츠 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 클래스에는 Cursor의 콘텐츠 URI에 연결된 파일의 이름과 크기를 반환하는 query() 메서드의 기본 구현이 있습니다. 기본 구현에서는 다음과 같은 두 개의 열을 반환합니다.

DISPLAY_NAME
String 형식의 파일 이름입니다. 이 값은 File.getName()에서 반환된 값과 동일합니다.
SIZE
long 형식의 파일 크기(바이트)입니다. 이 값은 File.length()에서 반환된 값과 동일합니다.

클라이언트 앱은 콘텐츠 URI를 제외한 query()의 모든 인수를 null로 설정하여 파일의 DISPLAY_NAMESIZE를 모두 가져올 수 있습니다. 예를 들어 다음 코드 스니펫은 파일의 DISPLAY_NAMESIZE를 가져와 개별 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)));
        ...
    

추가 정보는 다음을 참조하세요.