android StringBuilder在sdcard上保存为.txt文件

时间:2014-01-14 12:23:32

标签: android sd-card stringbuilder calllog

我需要帮助! 有人可以在这看到我的代码行。目前我正在编写一个代码来将calllogs保存到.txt文件中并将其保存到sdcard。它可以运行但不能将stringbuilder变量保存到sdcard。我是android编程的新手。

package com.example.calllog;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Date;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

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

        Cursor mCursor = managedQuery(CallLog.Calls.CONTENT_URI, null, null,
                null, null);
        int number = mCursor.getColumnIndex(CallLog.Calls.NUMBER);
        int date = mCursor.getColumnIndex(CallLog.Calls.DATE);
        int duration = mCursor.getColumnIndex(CallLog.Calls.DURATION);
        int type = mCursor.getColumnIndex(CallLog.Calls.TYPE);
        StringBuilder sb = new StringBuilder();
        while (mCursor.moveToNext()) {
            String phnumber = mCursor.getString(number);
            String callduration = mCursor.getString(duration);
            String calltype = mCursor.getString(type);
            String calldate = mCursor.getString(date);
            Date d = new Date(Long.valueOf(calldate));
            String callTypeStr = "";
            switch (Integer.parseInt(calltype)) {
            case CallLog.Calls.OUTGOING_TYPE:
                callTypeStr = "Outgoing";
                break;
            case CallLog.Calls.INCOMING_TYPE:
                callTypeStr = "Incoming";
                break;
            case CallLog.Calls.MISSED_TYPE:
                callTypeStr = "Missed";
                break;
            }

            sb.append("Phone number " + phnumber);
            sb.append(System.getProperty("line.separator"));
            sb.append("Call duration " + callduration);
            sb.append(System.getProperty("line.separator"));
            sb.append("Call type " + callTypeStr);
            sb.append(System.getProperty("line.separator"));
            sb.append("Call date " + d);
            sb.append("---------------------------");
            sb.append(System.getProperty("line.separator"));

        }



    try{

        File sdcard = new File("/mnt/extSdCard/");      
        File myFile = new File(sdcard.getAbsolutePath() + "/myForensicFile");
        myFile.mkdirs();
        File file = new File(myFile,"Call Logs.txt");
        FileOutputStream fOut = new FileOutputStream(file);
        OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
        myOutWriter.write(sb.toString());
        myOutWriter.close();
        Toast.makeText(getBaseContext(), "Call Logs Data saved to SDcard 'myForensicFile.txt'",Toast.LENGTH_LONG).show();
    }catch (Exception e){
        Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
    }

}
}

0 个答案:

没有答案