所以我已经查找了几种在Android中写入SD卡的方法,但似乎没有一种方法可以实际写入。我已经在清单中设置了权限。我写的我的FileIO类:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
//import android.app.Activity;
//import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
public class FileIO {
private static final String TAG = "FileIO";
private String filename;
private File sdCard;
private File dir;
public FileIO(String path, String filename){
Log.d(TAG, "Creating new File IO");
this.filename = filename;
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
Log.d(TAG, "Read and Write OK");
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
Log.w(TAG, "Read only OK");
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
Log.w(TAG, "Read and Write BLOCKED");
}
sdCard = Environment.getExternalStorageDirectory();
Log.d(TAG, "Writing to file: " + sdCard.getAbsolutePath()+path);
dir = new File (sdCard.getAbsolutePath()+path);
if (dir.mkdirs() || dir.isDirectory()) {
Log.d(TAG, "SUCCESS - Created directory");
} else {
Log.d(TAG, "FAILED - Create directory");
}
}
public void writeToFile(String s){
File file = new File(dir,this.filename);
try {
FileOutputStream f = new FileOutputStream(file,true); //True = Append to file, false = Overwrite
OutputStreamWriter osw = new OutputStreamWriter(f);
osw.write(s);
osw.flush();
f.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.printf("\nFile not found. Make sure to add WRITE_EXTERNAL_STORAGE permission to the manifest");
} catch (IOException e) {
e.printStackTrace();
}
}
}
然后这是它正在使用的类。
import android.app.Activity;
import android.text.format.DateFormat;
import android.widget.TextView;
public class AudioClipLogWrapper implements AudioClipListener
{
private TextView log;
private Activity context;
private double previousFrequency = -1;
private int previousVolume = -1;
private int previousMax = -1;
private FileIO fileIO;
public AudioClipLogWrapper(TextView log, Activity context)
{
this.log = log;
this.context = context;
String dateStamp = (DateFormat.format("dd-MM-yyyy-hh-mm-ss", new java.util.Date()).toString());
fileIO = new FileIO("/Android/data/com.uni.Lab7/files/",String.format("audio-%s.txt",dateStamp));
}
@Override
public boolean heard(short[] audioData, int sampleRate)
{
final double freq = ZeroCrossing.calculate(sampleRate, audioData);
final int maxAmplitude = AudioUtil.getMaxValue(audioData);
final double volume = AudioUtil.rootMeanSquared(audioData);
final StringBuilder message = new StringBuilder();
if ((((int)volume) > (4 * previousVolume)) && (maxAmplitude > (4 * previousMax)) ) {
message.append(" Clap!");
}
previousVolume = (int) volume;
previousMax= (int) maxAmplitude;
fileIO.writeToFile(String.format("%d, %d, %f\n",(int)volume, maxAmplitude, freq));
context.runOnUiThread(new Runnable()
{
@Override
public void run()
{
AudioTaskUtil.appendToStartOfLog(log, message.toString());
}
});
return false;
}
}
当我查看日志时,它表明一切都应该有效:
01-12 23:21:55.815 2752-2752/com.uni.Lab7 D/FileIO﹕ Creating new File IO
01-12 23:21:55.825 2752-2752/com.uni.Lab7 D/FileIO﹕ Read and Write OK
01-12 23:21:55.825 2752-2752/com.uni.Lab7 D/FileIO﹕ Writing to file: /storage/sdcard0/Android/data/com.uni.Lab7/files/
01-12 23:21:55.835 2752-2752/com.uni.Lab7 D/FileIO﹕ SUCCESS - Created directory
但是当我从手机中取出SD卡时,我在电脑上看到没有文件夹也没有文件。我查看了Android write to sd card folder和Write a file in SDcard in Android,我得到了他们的建议。
手机是运行4.1.2的三星GT-S53110B。因此,在我的gradle和manifest中,我将SDK的最小值设为8,目标为16。