Truy xuất thông tin tệp

Trước khi một ứng dụng khách cố gắng xử lý một tệp có URI nội dung, ứng dụng đó có thể yêu cầu thông tin về tệp từ ứng dụng máy chủ, bao gồm cả loại dữ liệu và kích thước tệp của tệp. Loại dữ liệu giúp ứng dụng khách xác định xem có thể xử lý tệp hay không, đồng thời kích thước tệp giúp ứng dụng thiết lập việc lưu vào bộ đệm và bộ nhớ đệm cho tệp.

Bài học này minh hoạ cách truy vấn FileProvider của ứng dụng máy chủ để truy xuất kích thước và loại MIME của tệp.

Truy xuất loại MIME của tệp

Loại dữ liệu của tệp cho ứng dụng khách biết cách ứng dụng xử lý nội dung của tệp. Để lấy loại dữ liệu của một tệp dùng chung dựa trên URI nội dung của tệp đó, ứng dụng khách sẽ gọi ContentResolver.getType(). Phương thức này trả về loại MIME của tệp. Theo mặc định, FileProvider xác định loại MIME của tệp dựa trên đuôi tên tệp.

Đoạn mã sau đây minh hoạ cách một ứng dụng khách truy xuất loại MIME của một tệp sau khi ứng dụng máy chủ trả về URI nội dung cho ứng dụng khách:

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

Truy xuất tên và kích thước của tệp

Lớp FileProvider có cách triển khai mặc định của phương thức query() trả về tên và kích thước của tệp liên kết với một URI nội dung trong Cursor. Phương thức triển khai mặc định trả về 2 cột:

DISPLAY_NAME
Tên của tệp, dưới dạng String. Giá trị này giống với giá trị được File.getName() trả về.
SIZE
Kích thước của tệp tính bằng byte, vì long Giá trị này giống với giá trị do File.length() trả về

Ứng dụng khách có thể nhận cả DISPLAY_NAMESIZE cho một tệp bằng cách đặt tất cả các đối số của query() thành null ngoại trừ URI nội dung. Ví dụ: đoạn mã này truy xuất DISPLAY_NAMESIZE của một tệp, đồng thời hiển thị từng phần tử trong TextView riêng biệt:

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

Để biết thêm thông tin liên quan, hãy tham khảo: