我有一个方法,它应该根据所选文件URI
返回contentResolver返回的文件名和文件大小,但有时所选文件没有扩展名。我正在寻找一种方法来获得完整的文件名及其扩展名,或者以某种方式确定文件类型。以下是我的方法我目前有
//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();
}
}
我正在寻找一种方法来获得完整的文件名及其扩展名
OpenableColumns.DISPLAY_NAME
不必是文件名。
或者确定文件类型的方法
在ContentResolver
上调用getType()
,提供Uri
。这应该为Uri
返回一个有效的MIME类型。