1 page.title=Retrieving File Information 2 3 trainingnavtop=true 4 @jd:body 5 6 7 <div id="tb-wrapper"> 8 <div id="tb"> 9 10 <!-- table of contents --> 11 <h2>This lesson teaches you to</h2> 12 <ol> 13 <li><a href="#RetrieveMimeType">Retrieve a File's MIME Type</a></li> 14 <li><a href="#RetrieveFileInfo">Retrieve a File's Name and Size</a></li> 15 </ol> 16 17 <!-- other docs (NOT javadocs) --> 18 <h2>You should also read</h2> 19 <ul> 20 <li><a href="{@docRoot}guide/topics/providers/content-provider-basics.html#SimpleQuery" 21 >Retrieving Data from the Provider</a></li> 22 </ul> 23 24 </div> 25 </div> 26 <p> 27 Before a client app tries to work with a file for which it has a content URI, the app can 28 request information about the file from the server app, including the file's data type and 29 file size. The data type helps the client app to determine if it can handle the file, and the 30 file size helps the client app set up buffering and caching for the file. 31 </p> 32 <p> 33 This lesson demonstrates how to query the server app's 34 {@link android.support.v4.content.FileProvider} to retrieve a file's MIME type and size. 35 </p> 36 <h2 id="RetrieveMimeType">Retrieve a File's MIME Type</h2> 37 <p> 38 A file's data type indicates to the client app how it should handle the file's contents. To get 39 the data type of a shared file given its content URI, the client app calls 40 {@link android.content.ContentResolver#getType ContentResolver.getType()}. This method returns 41 the file's MIME type. By default, a 42 {@link android.support.v4.content.FileProvider} determines the file's MIME type from its 43 filename extension. 44 </p> 45 <p> 46 The following code snippet demonstrates how a client app retrieves the MIME type of a file once 47 the server app has returned the content URI to the client: 48 </p> 49 <pre> 50 ... 51 /* 52 * Get the file's content URI from the incoming Intent, then 53 * get the file's MIME type 54 */ 55 Uri returnUri = returnIntent.getData(); 56 String mimeType = getContentResolver().getType(returnUri); 57 ... 58 </pre> 59 <h2 id="RetrieveFileInfo">Retrieve a File's Name and Size</h2> 60 <p> 61 The {@link android.support.v4.content.FileProvider} class has a default implementation of the 62 {@link android.support.v4.content.FileProvider#query query()} method that returns the 63 name and size of the file associated with a content URI in a 64 {@link android.database.Cursor}. The default implementation returns two columns: 65 </p> 66 <dl> 67 <dt>{@link android.provider.OpenableColumns#DISPLAY_NAME DISPLAY_NAME}</dt> 68 <dd> 69 The file's name, as a {@link java.lang.String}. This value is the same as the value returned 70 by {@link java.io.File#getName File.getName()}. 71 </dd> 72 <dt>{@link android.provider.OpenableColumns#SIZE SIZE}</dt> 73 <dd> 74 The size of the file in bytes, as a {@code long} This value is the same as the value 75 returned by {@link java.io.File#length File.length()} 76 </dd> 77 </dl> 78 <p> 79 The client app can get both the {@link android.provider.OpenableColumns#DISPLAY_NAME 80 DISPLAY_NAME} and {@link android.provider.OpenableColumns#SIZE SIZE} for a file by setting all 81 of the arguments of {@link android.support.v4.content.FileProvider#query query()} to 82 {@code null} except for the content URI. For example, this code snippet retrieves a file's 83 {@link android.provider.OpenableColumns#DISPLAY_NAME DISPLAY_NAME} and 84 {@link android.provider.OpenableColumns#SIZE SIZE} and displays each one in separate 85 {@link android.widget.TextView}: 86 </p> 87 <pre> 88 ... 89 /* 90 * Get the file's content URI from the incoming Intent, 91 * then query the server app to get the file's display name 92 * and size. 93 */ 94 Uri returnUri = returnIntent.getData(); 95 Cursor returnCursor = 96 getContentResolver().query(returnUri, null, null, null, null); 97 /* 98 * Get the column indexes of the data in the Cursor, 99 * move to the first row in the Cursor, get the data, 100 * and display it. 101 */ 102 int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); 103 int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE); 104 returnCursor.moveToFirst(); 105 TextView nameView = (TextView) findViewById(R.id.filename_text); 106 TextView sizeView = (TextView) findViewById(R.id.filesize_text); 107 nameView.setText(returnCursor.getString(nameIndex)); 108 sizeView.setText(Long.toString(returnCursor.getLong(sizeIndex))); 109 ... 110 </pre> 111