2012-01-02 28 views
4

ボタンを押すと同じテキストを含むSMSメッセージを受信すると、SDカードにテキストファイルを書き込むアンドロイドアプリケーションを作成しています。AndroidのSDカードからの書き込みとファイルの読み込み

SMSを受信すると、テキストファイルをSDカードに保存してそのテキストファイルから読み込むにはどうすればよいですか?これは私がこれまで持っているコードです:

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.Environment; 
import android.telephony.gsm.SmsMessage; 
import android.widget.Toast; 
import android.media.MediaPlayer; 

public class IncomingSmsCaptureApp extends BroadcastReceiver { 
MediaPlayer mp1; 
@Override 
public void onReceive(Context context, Intent intent) { 
File sdcard = Environment.getExternalStorageDirectory(); 

//Get the text file 
File file = new File(sdcard,"Notes\file.txt"); 

//Read text from file 
String text = new String(); 

try { 
    BufferedReader br = new BufferedReader(new FileReader(file)); 
    String line; 

    while ((line = br.readLine()) != null) { 
    } 
} 
catch (IOException e) { 
    //You'll need to add proper error handling here 
} 
//---get the SMS message passed in--- 
Bundle bundle = intent.getExtras();  
SmsMessage[] msgs = null; 
String str = "";  
String Message = ""; 
if (bundle != null) 
{ 
//---retrieve the SMS message received--- 
Object[] pdus = (Object[]) bundle.get("pdus"); 
msgs = new SmsMessage[pdus.length];   
for (int i=0; i<msgs.length; i++){ 
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);    
str += "SMS from " + msgs[i].getOriginatingAddress();      
str += " :"; 
str += msgs[i].getMessageBody().toString(); 
str += "\n";  
Message = msgs[i].getMessageBody().toString(); 
} 
//---display the new SMS message--- 
Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); 
if (Message.equals("alarm")) { 
//Play alarm sound 
mp1 = MediaPlayer.create(context, R.raw.alarm); 
mp1.start(); 
} 
else { 
if (Message.equals(text)) { 
    //Perform action 
} 
} 
}  
} 
} 
+0

...あなたの質問は? – stefan

+0

なぜ人々は常にたくさんのコードを投げ、他の人がそれを読んでいると思いますか?つまり、なぜ機能しない関連するコードを私たちに与えることができないのですか? – ariefbayu

+0

私は知っている必要がありますテキストファイルからSDカードにテキストファイルを書き込む方法 – MySoftware

答えて

7

次のコードは、あなたが最初にあなたのTextView

からテキストを取得するルートディレクトリに

を、それを提出し、保存するためのテキストを書くのに役立ちます

String text = myTextView.getText().toString(); 

そして、テキストファイルに

File logFile = new File(Environment.getExternalStorageDirectory().toString(), "myFile.txt"); 
if(!logFile.exists()) { 
    logFile.createNewFile(); 
} 

BufferedWriter output = new BufferedWriter(new FileWriter(logFile)); 
output.write(text); 
output.close(); 

してファイルを書き込むために、次の文を書きますルートディレクトリに保存されます

いけないのWRITE_EXTERNAL_STORAGE権限を追加することを忘れマニフェスト

+0

+1 - すべての周りの良い答え。 –

+0

ファイルとは何ですか?私はそれの下にこれを得る:ファイルは変数に解決することはできません。それは新しいファイルライター – MySoftware

+0

ソーシャル私の間違い "ファイル"を "ログファイル"で置き換えた直後です... –

1

ここにあなたのSDカード上のファイルへのTextViewのテキストを書き込み、いくつかの基本的なコードがあります。

File myFile = new File(Environment.getExternalStorageDirectory().toString(), "myFile.txt"); 
myFile.createNewFile(); 
FileOutputStream fOut = new FileOutputStream(myFile); 
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); 
myOutWriter.append(txtData.getText()); 
myOutWriter.close(); 
fOut.close(); 

希望します。

関連する問題