Zanim aplikacja kliencka spróbuje pracować z plikiem, który ma identyfikator URI treści, może zażądać od aplikacji serwera informacji o tym pliku, w tym o typie i rozmiarze pliku. Typ danych pomaga aplikacji klienckiej określić, czy może obsłużyć plik, a rozmiar pliku pomaga aplikacji klienckiej skonfigurować buforowanie i zapisywanie pliku w pamięci podręcznej.
Ta lekcja pokazuje, jak wysłać zapytanie do FileProvider
aplikacji serwera, aby pobrać typ MIME i rozmiar pliku.
Pobieranie typu MIME pliku
Typ danych pliku wskazuje aplikacji klienckiej jak powinna postępować z jego zawartością. Aby uzyskać typ danych udostępnionego pliku ze względu na jego identyfikator URI treści, aplikacja kliencka wywołuje metodę ContentResolver.getType()
. Ta metoda zwraca typ MIME pliku. Domyślnie FileProvider
określa typ MIME pliku na podstawie jego rozszerzenia.
Ten fragment kodu ilustruje, jak aplikacja kliencka pobiera typ MIME pliku, gdy aplikacja serwera zwróci klientowi identyfikator URI treści:
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); ...
Pobieranie nazwy i rozmiaru pliku
Klasa FileProvider
ma domyślną implementację metody query()
, która zwraca nazwę i rozmiar pliku powiązanego z identyfikatorem URI treści w obiekcie Cursor
. Domyślna implementacja zwraca 2 kolumny:
DISPLAY_NAME
- Nazwa pliku w postaci
String
. Ta wartość jest taka sama jak wartość zwrócona przez funkcjęFile.getName()
. SIZE
- Rozmiar pliku w bajtach, jako
long
Ta wartość jest taka sama jak wartość zwrócona przezFile.length()
Aplikacja kliencka może pobrać zarówno DISPLAY_NAME
, jak i SIZE
pliku, ustawiając wszystkie argumenty query()
na null
oprócz identyfikatora URI treści. Na przykład ten fragment kodu pobiera atrybuty DISPLAY_NAME
i SIZE
z pliku i wyświetla każdą z nich w osobnych elementach 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))); ...
Dodatkowe informacje na ten temat znajdziesz tutaj: