如何使用Intent.EXTRA_TEXT发送SQLite数据库的电子邮件?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用Intent.EXTRA_TEXT发送SQLite数据库的电子邮件?相关的知识,希望对你有一定的参考价值。

我想简单地发送一封电子邮件,其中包含我的数据库中的数据作为电子邮件中的文本。

我搜索了大量的帖子,并且不想作为附件发送,我不想保存到外部存储器等,也无法找到我要找的东西。

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

    @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 + "
" + date + "
" + destination + "
" + purpose + "
" + mileage);
                startActivity(Intent.createChooser(emailIntent, "Select App"));
答案

感谢CommonsWare回答这个问题。我确实使用了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("
");

                        //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 + "
" + date + "
" + destination + "
" + purpose + "
" + mileage);
                startActivity(Intent.createChooser(emailIntent, "Select App"));

以上是关于如何使用Intent.EXTRA_TEXT发送SQLite数据库的电子邮件?的主要内容,如果未能解决你的问题,请参考以下文章

意图发送崩溃

小功能整理

Android 从 Intent 获取 vCard 数据

你能把多个extra_text's放在一个android emailIntent中吗

我如何与 Intent.ACTION_SEND 一起共享和保存图像和文本?

Android:分享列表?