LoginSignup
11
12

More than 5 years have passed since last update.

CSVを作成してGmailに添付する方法

Last updated at Posted at 2014-09-30

Gmailのみの対応

CSVファイルを作成

GmailにIntentを投げる のシンプルな流れ

String tmpCsvPath = Environment.getExternalStorageDirectory().getPath() + "/tmp.csv"; 
try {
    // csvファイル作成
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tmpCsvPath, false), "SJIS"));
    bw.write("test1,");
    bw.write("test2");
    bw.newLine();
    for (int i=0;i<=10000;i++) {
        bw.write((i + 1) + ",");;
        bw.write(Math.random()*10+"");
        bw.newLine();
    }
    bw.flush();
    bw.close();

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_SUBJECT, "");
    intent.putExtra(Intent.EXTRA_TEXT, "");
    // Gmailにファイルの添付
    Uri attachments = Uri.parse("file://" + tmpCsvPath);
    intent.putExtra(Intent.EXTRA_STREAM, attachments);
    intent.setType("application/*");
    intent.setPackage("com.google.android.gm");
    try {
        startActivity(intent);
    } catch (android.content.ActivityNotFoundException ex) {
        ex.printStackTrace();
        Toast.makeText(this.getApplicationContext(), "client not found", Toast.LENGTH_LONG).show();
    }
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

※追記
AndroidManifest.xmlに以下も追加

<!-- 外部SDカードへの書き込み -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
11
12
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
12