Skip to content

Instantly share code, notes, and snippets.

@ueno-yuhei
Last active August 29, 2015 14:01
Show Gist options
  • Save ueno-yuhei/f19c18f7857696e8fd62 to your computer and use it in GitHub Desktop.
Save ueno-yuhei/f19c18f7857696e8fd62 to your computer and use it in GitHub Desktop.
Table生成 csv作成 Gmail添付
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
private ExampleOpenHelper mRadiationOpenHelper;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mExampleOpenHelper = new ExampleOpenHelper(this);
SQLiteDatabase db = mExampleOpenHelper.getReadableDatabase(); //データベース取得
/* データの書き込み */
String FILE = "/sdcard/sv_h.csv";
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(FILE, true), "UTF-8"));
Cursor cursor = db.rawQuery("SELECT * from example", new String[] {});
boolean next = cursor.moveToFirst();
while (next) {
bw.write(cursor.getString(1));
bw.newLine();
next = cursor.moveToNext();
}
bw.close();
cursor.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setData(Uri.parse("mailto:ueno@logiclogic.jp"));
intent.putExtra(Intent.EXTRA_SUBJECT, "XXX");
intent.putExtra(Intent.EXTRA_TEXT, "本文");
// ファイルの添付
File externalSD = new File(System.getenv("EXTERNAL_STORAGE"));
String file_path = "/sdcard/sv_h.csv";
Uri attachments = Uri.parse("file://"+file_path);
intent.putExtra(Intent.EXTRA_STREAM, attachments);
intent.setType("application/*");
try {
startActivity(intent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "client not found", Toast.LENGTH_LONG).show();
}
db.close();
mRadiationOpenHelper.close();
}
private class ExampleOpenHelper extends SQLiteOpenHelper {
final static private int DB_VERSION = 1;
public ExampleOpenHelper(Context context) {
super(context, "sample.db", null, DB_VERSION);
}
// 初回起動時のみ処理される
@Override
public void onCreate(SQLiteDatabase db) {
/**
* name: exampleテーブル
* id:integer | memo:real
*/
db.execSQL("CREATE TABLE example(id INTEGER PRIMARY KEY AUTOINCREMENT, memo REAL NOT NULL);");
// 初期データの投入
for(int i=0;i<=1000;i++){
db.execSQL("INSERT INTO example(memo) values('"+ Math.random() +"')");
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment