Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing and a file reading from the SD Card in Android

I am making an android application that will write a text file to the SD Card from a textview once a button is pressed and once an SMS message is received containing the same text.

How do I save a text file to the SD-Card and read from that text file once an SMS is received? This is the code that I've got so far:

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
}
}
}       
}
}
like image 753
MySoftware Avatar asked Feb 22 '23 23:02

MySoftware


2 Answers

The following code will help you write text to file and save it in the root directory

Firstly get the text from your textview

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

Then write the following statements to write into a text file

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

And your File will be stored in the root directory

Dont forget to add WRITE_EXTERNAL_STORAGE permission to your manifest

like image 140
King RV Avatar answered Feb 24 '23 13:02

King RV


Here's some basic code that writes text from a TextView to a file on your SD card.

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

Hope this helps!

like image 23
Mat Nadrofsky Avatar answered Feb 24 '23 12:02

Mat Nadrofsky