2012-01-02 96 views
4

我正在製作一個android應用程序,一旦按下按鈕並收到包含相同文本的SMS消息,就會從textview中向SD卡寫入文本文件。Android中的SD卡的寫入和文件讀取

一旦收到短信,我如何將文本文件保存到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 2012-01-02 14:51:48

+0

爲什麼人們總是拋出一堆代碼,並認爲其他人會通讀它?我的意思是,爲什麼你不能只給我們一些不起作用的相關代碼呢? – ariefbayu 2012-01-02 15:06:51

+0

我需要知道如何將文本文件從文本框中寫入SD卡 – MySoftware 2012-01-02 15:22:15

回答

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 - 四周的好答案。 – 2012-01-02 15:46:03

+0

什麼是文件?我得到它下面:文件不能解析爲一個變量。在新的文件編寫器 – MySoftware 2012-01-02 16:02:38

+0

sory之後,我的錯誤將「file」替換爲「logFile」... – 2012-01-02 16:55:17

1

下面是從一個TextView你的SD卡上寫入文本文件中的一些基本代碼。

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(); 

希望這有助於!