【问题标题】:Writing and a file reading from the SD Card in Android在 Android 中从 SD 卡写入和读取文件
【发布时间】:2012-01-02 14:48:33
【问题描述】:

我正在制作一个 android 应用程序,一旦按下按钮并收到包含相同文本的 SMS 消息,它将从 textview 将文本文件写入 SD 卡。

如何将文本文件保存到 SD 卡并在收到 SMS 后从该文本文件中读取?这是我目前得到的代码:

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
}
}
}       
}
}

【问题讨论】:

  • ...你的问题是?
  • 为什么人们总是抛出一堆代码并认为其他人只会读它?我的意思是,你为什么不能给我们一段不起作用的相关代码?
  • 我需要知道如何将文本文件从文本框写入 SD 卡

标签: android storage android-sdcard


【解决方案1】:

以下代码将帮助您将文本写入文件并保存在根目录中

首先从你的文本视图中获取文本

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 权限

【讨论】:

  • 什么是文件?我得到了它:文件无法解析为变量。紧接着新的文件编写器
【解决方案2】:

下面是一些将文本从 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();

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2014-07-05
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 2015-08-03
    • 2018-10-03
    相关资源
    最近更新 更多