1

How to send Listview items to email as attachment, preferably as csv? Below is a sample of the Listview items, which I would like to send them by email as attachment:

public class MainActivity extends AppCompatActivity {

ListView listView;

// Define string array.
String[] listValue = new String[] {"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT"};



@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView)findViewById(R.id.listView1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_2, android.R.id.text1, listValue);

    listView.setAdapter(adapter);

   

}

}

I have checked with some pertinent SO comments. this code is to export to a SDCard. What I wish is to directly send them without storing them to a SDCard. This comment shows how to send them as part of the message body. My intention is to send them as direct attachment. I would very much appreciate for helps in implementing this or for guiding me to some useful link or tutorial to achieve this.

EDIT (to add a code with which I am trying to access a data (saved Listview items as CSV) and attach them:

On buttonClick:

File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); 
String file = "/Data.CSV";
Uri uri = Uri.parse(directory+ file);
sendMail(getContext(), mailID, mailSubject, null, uri);

sendmail Function:

public void sendMail(Context context, String mailID, String subject, File attachment, Uri uri) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra(Intent.EXTRA_EMAIL, mailID);
    // Need to grant this permission
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    // Attachment
    intent.setType("vnd.android.cursor.dir/email");

    if (attachment != null)
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(attachment));
    else if (uri != null)
        intent.putExtra(Intent.EXTRA_STREAM, uri);

    
}

This function is not working

tams
  • 11
  • 2
  • email "attachments" are placed after the body as a "part" usually crypted to base 64 if it is a file , of which it invariably becomes when it is an attachment and given a "part" content type of "attachment". Just as any data the API for the mail message has a class and method to load an attachment to the message whether data or file. Because it becomes a file it also needs a name and extension. – Samuel Marchant Jun 30 '21 at 07:38
  • @@Samuel, you mean that we cannot send the items on the fly, is that right? Because, to "load" mean there must be a file stored in the device. What best way would you suggest? I don't want to use SDCard (external storage). If no other possibilities, I would consider Internal storage (download folder) – tams Jun 30 '21 at 07:46
  • If you have the "data" then you can send it, and if you can create a mail message body from the API and load the body object to the message object, then you can load the "data" to the mail API method to load an attachment ! YOU CAN whether the data came from file or is data but it will BECOME a file if it is an attachment. Use the specified API methods (and base 64 on the whole single data string and make a default file name and extension) – Samuel Marchant Jun 30 '21 at 08:28
  • ...AND NB: The body may only be "here is your data" or even empty "" string. – Samuel Marchant Jun 30 '21 at 08:31
  • @@Samuel: I can get the data from the internal storage. I tried to access and attach the file with: File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); String file = "/Data.CSV";Uri uri = Uri.parse(directory+ file);sendMail(getContext(), mailID, mailSubject, null, uri); I am editing my question to add the SendMail function, which is not helping much. Please have a look at it in case a fix is possible – tams Jun 30 '21 at 09:09
  • Which class library and version does the sendMail(...) method come from ? e.g. in javamail 1.3.3 send(Message, Address[]) – Samuel Marchant Jul 01 '21 at 05:58
  • NOTE TOO: If you are having trouble you should wrap the code in a try{}catch(Exception exm){exm.printStackTrace();} and if the other methods it uses requires them if compiling (the compiler output)calls to, then wrap their contents but be aware to sit return variables and other more AKA global vars inside above the try when instantiating them. (NOTE: Always wrap File in FileNotFoundException and IOException after the other exception of which Exception is always last in the chain) – Samuel Marchant Jul 01 '21 at 06:03

0 Answers0