代码之家  ›  专栏  ›  技术社区  ›  Joshua Edgar

如何使用Intent发送SQLite数据库的电子邮件。额外的\u文本?

  •  0
  • Joshua Edgar  · 技术社区  · 6 年前

    我试图简单地发送一封电子邮件,将数据库中的数据作为电子邮件中的文本。

    我已经搜索了大量帖子,不想作为附件发送,不想保存到外部存储等,也找不到我要找的内容。

    我希望填充数据库中的所有行,但只能获得一行。任何帮助都将不胜感激。

        @Override
                public void onClick(DialogInterface dialogInterface, int i) {
    
                    String[] projection = {
                            LogEntry._ID,
                            LogEntry.COLUMN_LOG_DATE,
                            LogEntry.COLUMN_LOG_DESTINATION,
                            LogEntry.COLUMN_LOG_PURPOSE,
                            LogEntry.COLUMN_LOG_MILEAGE};
    
    
                    Cursor cursor = getContentResolver().query(
                            LogEntry.CONTENT_URI,
                            projection,
                            null,
                            null,
                            null);
    
                    //TODO ???????????? I want the cursor to continue and get all the data from the database???
                    //This is the current result in the email.
                    // 1
                    // 05 Mar 2018
                    // 12:10:52 PM
                    // 0
                    // 0
                    // 88031
    
    
                    // Extract the properties from cursor
                    // Find columns of log attributes that I am interested in
                    int idColumnIndex = cursor.getColumnIndex(LogEntry._ID);
                    int dateColumnIndex = cursor.getColumnIndex(LogEntry.COLUMN_LOG_DATE);
                    int destinationColumnIndex = cursor.getColumnIndex(LogEntry.COLUMN_LOG_DESTINATION);
                    int purposeColumnIndex = cursor.getColumnIndex(LogEntry.COLUMN_LOG_PURPOSE);
                    int mileageColumnIndex = cursor.getColumnIndex(LogEntry.COLUMN_LOG_MILEAGE);
    
                    String id = cursor.getString(idColumnIndex);
                    String date = cursor.getString(dateColumnIndex);
                    int destination = cursor.getInt(destinationColumnIndex);
                    int purpose = cursor.getInt(purposeColumnIndex);
                    String mileage = cursor.getString(mileageColumnIndex);
    
    
                    Intent emailIntent = new Intent(Intent.ACTION_SEND);
                    emailIntent.setType("message/rfc822");
                    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"recipent@gmail.com"});
                    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "All Logs for " + LogEntry.COLUMN_LOG_DATE);
                    emailIntent.putExtra(Intent.EXTRA_TEXT, id + "\n" + date + "\n" + destination + "\n" + purpose + "\n" + mileage);
                    startActivity(Intent.createChooser(emailIntent, "Select App"));
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Joshua Edgar    6 年前

    感谢您的Commonware回答此问题。我确实使用了Stringbuilder,它工作得非常完美。正是我想要的。

    下面是代码。

         @Override
                public void onClick(DialogInterface dialogInterface, int i) {
    
                    StringBuilder stringBuilder = new StringBuilder();
    
                    String[] projection = {
                            LogEntry._ID,
                            LogEntry.COLUMN_LOG_DATE,
                            LogEntry.COLUMN_LOG_DESTINATION,
                            LogEntry.COLUMN_LOG_PURPOSE,
                            LogEntry.COLUMN_LOG_MILEAGE};
    
    
                    Cursor cursor = getContentResolver().query(
                            LogEntry.CONTENT_URI,
                            projection,
                            null,
                            null,
                            null);
    
                    //if the cursor isn't null we will essentially iterate over rows and then columns
                    //to form a table of data as per database.
                    if (cursor != null) {
    
                        //more to the first row
                        cursor.moveToFirst();
    
                        //iterate over rows
                        for (int index = 0; index < cursor.getCount(); index++) {
    
                            //iterate over the columns
                            for(int j = 0; j < cursor.getColumnNames().length; j++){
    
                                //append the column value to the string builder and delimit by a pipe symbol
                                stringBuilder.append(cursor.getString(j) + " | ");
                            }
                            //add a new line carriage return
                            stringBuilder.append("\n");
    
                            //move to the next row
                            cursor.moveToNext();
                        }
                        //close the cursor
                        cursor.close();
                    }
    
    
    
                    Intent emailIntent = new Intent(Intent.ACTION_SEND);
                    emailIntent.setType("message/rfc822");
                    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"recipent@gmail.com"});
                    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Logs for " + ;
                    emailIntent.putExtra(Intent.EXTRA_TEXT, stringBuilder.toString()); //id + "\n" + date + "\n" + destination + "\n" + purpose + "\n" + mileage);
                    startActivity(Intent.createChooser(emailIntent, "Select App"));