Android - get email attachment name in my application

后端 未结 4 1736
独厮守ぢ
独厮守ぢ 2020-12-18 02:16

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:

         


        
4条回答
  •  温柔的废话
    2020-12-18 02:21

    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 ""; }
    }
    

提交回复
热议问题