Reputation: 3167
I have a method that should return the file name and file size returned from the contentResolver based on a selected file URI
, however at times the file that was chosen has no extension. I am looking for a way to get the full file name with its extension or a way to determine the file type somehow. Below is my method I currently have
//This is the intent which triggers the file choosing
Intent fileIntent = new Intent( );
fileIntent.setType( "*/*" );
fileIntent.setAction( Intent.ACTION_GET_CONTENT );
fileIntent.putExtra( Intent.EXTRA_LOCAL_ONLY, true );
startActivityForResult( fileIntent, REQUEST_CODE_FILE );
//Method in onActivityResult
Uri selectedFile = intent.getData();
Cursor cursor = null;
String fileName = null;
int fileSize = 0;
try {
cursor = getContentResolver().query( selectedFile, null, null, null, null );
cursor.moveToFirst();
fileName = cursor.getString( cursor.getColumnIndex( OpenableColumns.DISPLAY_NAME ));
fileSize = cursor.getInt( cursor.getColumnIndex( OpenableColumns.SIZE ) );
}catch( Exception ex ){
ex.printStackTrace();
}finally {
if( cursor != null ) {
cursor.close();
}
}
Upvotes: 0
Views: 997
Reputation: 1006574
I am looking for a way to get the full file name with its extension
OpenableColumns.DISPLAY_NAME
does not have to be a filename.
or a way to determine the file type
Call getType()
on the ContentResolver
, supplying the Uri
. This should return a valid MIME type for the Uri
.
Upvotes: 1