ファイル情報の取得

クライアント アプリは、コンテンツ 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)
        }
        ...
    

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() メソッドのデフォルトの実装があります。これは、Cursor でコンテンツ URI に関連付けられたファイルの名前とサイズを返します。デフォルトの実装では 2 つの列が返されます。

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()
            ...
        }
    

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)));
        ...
    

その他の関連情報については、以下をご覧ください。