在寫自己的一個小密碼儲存項目中,要實作一個功能,将存在資料庫裡面的所有密碼資訊全部寫到SD中的某個檔案,然後将該檔案直接做成附件發送郵箱。
讀取資料(參考的http://blog.csdn.net/xinzheng_wang/article/details/7793692)
public static void ExportToCSV(Cursor c, String fileName) {
int rowCount = 0;
int colCount = 0;
FileWriter fw;
BufferedWriter bfw;
File sdCardDir = Environment.getExternalStorageDirectory();
Log.d("haha", sdCardDir.getAbsolutePath());//storage/emulated/0
File saveFile = new File(sdCardDir, fileName);
try {
rowCount = c.getCount();
colCount = c.getColumnCount();
fw = new FileWriter(saveFile);
bfw = new BufferedWriter(fw);
if (rowCount > 0) {
c.moveToFirst();
// 寫入表頭
for (int i = 0; i < colCount; i++) {
if (i != colCount - 1)
bfw.write(c.getColumnName(i) + ',');
else
bfw.write(c.getColumnName(i));
}
// 寫好表頭後換行
bfw.newLine();
// 寫入資料
for (int i = 0; i < rowCount; i++) {
c.moveToPosition(i);
// Toast.makeText(mContext, "正在導出第"+(i+1)+"條",
// Toast.LENGTH_SHORT).show();
Log.v("導出資料", "正在導出第" + (i + 1) + "條");
for (int j = 0; j < colCount; j++) {
if (j != colCount - 1)
bfw.write(c.getString(j) + ',');
else
bfw.write(c.getString(j));
}
// 寫好每條記錄後換行
bfw.newLine();
}
}
// 将緩存資料寫入檔案
bfw.flush();
// 釋放緩存
bfw.close();
// Toast.makeText(mContext, "導出完畢!", Toast.LENGTH_SHORT).show();
Log.v("導出資料", "導出完畢!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
c.close();
}
}
操作手機模拟器可以看到該檔案,然而下一步如何在程式中實作将該檔案作為附件傳出去還沒有想法額