Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android exporting to csv and sending as email attachment

Tags:

I have seen multiple threads in this site discussing about sending email with attachments in android. I tried every methods discussed here, here and here.

I am creating a csv file via code and saving this file to android internal storage. Then I want to send this file as attachment in an email. Well, the email is being sent, I am getting it without attachment. This is what I have done.

String columnString         =   "\"Person\",\"Gender\",\"Street1\",\"PostOfice\",\"Age\"";
String dataString           =   "\"" + currentUser.userName +"\",\"" + currentUser.gender + "\",\"" + currentUser.street1 + "\",\"" + currentUser.poNumber.toString() + "\",\"" + currentUser.age.toString() + "\"";
String combinedString       =   columnString + "\n" + dataString;
File file                   =   new File(this.getCacheDir()+ File.separator + "Data.csv");
try {
    FileOutputStream out    =   new FileOutputStream(file);
    out.write(combinedString.getBytes());
    out.close();
} catch (IOException e) {
    Log.e("BROKEN", "Could not write file " + e.getMessage());
}   
Uri u1                      =   Uri.fromFile(file);

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Person Details");
sendIntent.putExtra(Intent.EXTRA_STREAM, u1);
sendIntent.setType("text/richtext");
startActivity(sendIntent);

I tried changing mime settings to "text/html" and "text/richtext" etc. But no luck yet. Can anyone tell me what I am doing wrong?

like image 257
Krishnabhadra Avatar asked Mar 23 '11 05:03

Krishnabhadra


People also ask

Where do CSV files go on android?

Where to put the CSV file in Android. Create a folder named “raw” inside the “res” folder and put the CSV file in it.

What is CSV file in Android?

A CSV is a comma-separated values file, which allows data to be saved in a tabular format. CSV File Reader is an awesome app for viewing both small and large-sized CSV files. The app comes with a lot of interesting features; - Auto-discovery of all CSV files on your android device (Internal & External storage).


2 Answers

Thanks for everyone who tried to help..After taking a full day I have send an email from my app with attachment..This is the working code..

String columnString =   "\"PersonName\",\"Gender\",\"Street1\",\"postOffice\",\"Age\"";
String dataString   =   "\"" + currentUser.userName +"\",\"" + currentUser.gender + "\",\"" + currentUser.street1 + "\",\"" + currentUser.postOFfice.toString()+ "\",\"" + currentUser.age.toString() + "\"";
String combinedString = columnString + "\n" + dataString;

File file   = null;
File root   = Environment.getExternalStorageDirectory();
if (root.canWrite()){
    File dir    =   new File (root.getAbsolutePath() + "/PersonData");
     dir.mkdirs();
     file   =   new File(dir, "Data.csv");
     FileOutputStream out   =   null;
    try {
        out = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            out.write(combinedString.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Uri u1  =   null;
u1  =   Uri.fromFile(file);

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Person Details");
sendIntent.putExtra(Intent.EXTRA_STREAM, u1);
sendIntent.setType("text/html");
startActivity(sendIntent);

Also If you have mounted your phone SDCard in the machine , this code wont work. Only one can access SDCard at one time. So in that case unmount your SDCard from computer and try..Thanks to the guy who answered here..Also make sure you have bought permission to write to external Storage in your manifest file...

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

Hope it helps someone...Thanks for everyone who tried to help..

like image 86
Krishnabhadra Avatar answered Sep 20 '22 15:09

Krishnabhadra


This Code will help you out

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    buttonSend = (Button) findViewById(R.id.buttonSend);

    textTo = (EditText) findViewById(R.id.editTextTo);
    textSubject = (EditText) findViewById(R.id.editTextSubject);
    textMessage = (EditText) findViewById(R.id.editTextMessage);

    buttonSend.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            String to = textTo.getText().toString();
            String subject = textSubject.getText().toString();
            String message = textMessage.getText().toString();

            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("plain/text");
            File data = null;
            try {
                Date dateVal = new Date();
                String filename = dateVal.toString();
                data = File.createTempFile("Report", ".csv");
                FileWriter out = (FileWriter) GenerateCsv.generateCsvFile(
                        data, "Name,Data1");
                i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(data));
                i.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
                i.putExtra(Intent.EXTRA_SUBJECT, subject);
                i.putExtra(Intent.EXTRA_TEXT, message);
                startActivity(Intent.createChooser(i, "E-mail"));

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    });
}

public class GenerateCsv {
    public static FileWriter generateCsvFile(File sFileName,String fileContent) {
        FileWriter writer = null;

        try {
            writer = new FileWriter(sFileName);
            writer.append(fileContent);
                         writer.flush();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally
        {
            try {
                writer.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return writer;
    }
}

Add this line in AndroidManifest.xml file:

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission
like image 32
RajeshVijayakumar Avatar answered Sep 23 '22 15:09

RajeshVijayakumar