2

私はSMS処理のためにSMSに依存するAndroidバージョン2.3.3アプリケーションに取り組んでいます。SendSMSとReceiveSMSの2つのクラスを作成しました。送受信された各SMSをログに記録するログファイルを作成したいと思います。ログファイルを作成するためにコードを記述しましたが、機能しません。

以下は私のコードです、

public void WriteOnLog()
{
    File exportDir =  Environment.getExternalStorageDirectory();

    if (!exportDir.exists())          {              
    exportDir.mkdirs();          
    }   
    String fileName;    

        fileName = "log" + ".txt";

    File file = new File(exportDir,fileName); 
    String txt=null ;
    try {
        if(!file.exists()){
            file.createNewFile();
            FileWriter Write = new FileWriter(file,true);
            out = new BufferedWriter(Write);
             txt = "Date Time |" + " Send/Receive |" + " Controller No |" +" msg";
        }
        FileWriter Write = new FileWriter(file,true); 
        out = new BufferedWriter(Write);

        String dd=null,mm=null,yy=null,hh=null,min=null,ss=null,dt=null;    
        SimpleDateFormat sdfDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        String newtime =  sdfDateTime.format(new Date(System.currentTimeMillis()));             
        yy = newtime.substring(2, 4);
        mm = newtime.substring(5, 7);
        dd = newtime.substring(8, 10);
        hh = newtime.substring(11, 13);
        min = newtime.substring(14, 16);
        ss = newtime.substring(17); 
        dt = dd+"-"+mm+"-"+yy +" " + hh + ":" + min +":"+ ss;
        txt = dt + " |" +" Send " + "| " + phoneNumber + " | " + message.toUpperCase();
        out.write(txt + "\n");     


    }          
    catch(IOException sqlEx)   {              
    Log.e("MainActivity", sqlEx.getMessage(), sqlEx);              

}
}
4

1 に答える 1

0

私は(FileWriterの代わりに)OutputStreamWriterアプローチを使用しました。私はそれを開発環境で動作させ、デバイス(Android 2.3.6)でテストしました。これを試してください:

try {
        File directory;
        if(isSDCard){
            directory = new File(Environment.getExternalStorageDirectory().toString()+"/"+folderNameOrPath+"/");
        }
        else{
            directory = new File(folderNameOrPath);
        }

        boolean fileReturnVal = directory.mkdirs();
        if(fileReturnVal){
            Log.d("Storage", "Write in SD Card: " + folderNameOrPath + " folder Created successfully");
        }
        else{
            Log.d("Storage", "Write in SD Card: " + folderNameOrPath + " folder either already exists or creation failed");
        }

        File file = new File(directory, fileNameWithExtention);
        FileOutputStream fOut = new FileOutputStream(file);

        OutputStreamWriter osw = new OutputStreamWriter(fOut);

        //Write the string to the file
        osw.write(text);
        osw.flush();
        osw.close();

        //file saved
        Toast.makeText(context, "Text saved in SD Card", Toast.LENGTH_SHORT).show();
        return true;

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        Log.e("Storage", "Write in SD Card: File Not Found ERROR: " + e.toString());
        return false;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.e("Storage", "Write in SD Card: IOException ERROR: " + e.toString());
        return false;
    } catch (Exception e) {
        Log.e("Storage", "Write in SD Card: ERROR: " + e.toString());
        return false;
    }

タスクの追加モードを設定できます。変数は SDCard、folderName、fileNameWithExtn です。などはメソッドレベルの変数です。私はそれらをパラメータとして渡しています。

于 2012-05-29T06:29:24.990 に答える