I\'m trying to load an email attachment in my application. I can get the content, but I cannot get the file name.
Here\'s how my intent filter looks like:
Here's a "combo solution". Note the final fallback using the mimeType. You will need to define the toExtension() method...
private static String contentUriFilename( Uri uri ){
String fileName="";
if( fileName.isEmpty() )
fileName = contentUriFilename( uri, android.provider.OpenableColumns.DISPLAY_NAME );
if( fileName.isEmpty() )
fileName = contentUriFilename( uri, android.provider.MediaStore.MediaColumns.DISPLAY_NAME );
if( fileName.isEmpty() )
fileName = contentUriFilename( uri, "_display_name" );
if( fileName.isEmpty() ){
String mimeType;
try{ mimeType = context_.getContentResolver().getType( uri ); }
catch( Exception e ){ mimeType=""; }
fileName = "unknown." + toExtension( mimeType );
}
return fileName;
}
private static String contentUriFilename( Uri uri, String columnName ){
Cursor cursor = context_.getContentResolver().query( uri,
new String[]{columnName}, null, null, null);
cursor.moveToFirst();
try{
return cursor.getString(
cursor.getColumnIndexOrThrow( columnName ) );
}
catch( Exception e ){ return ""; }
}