我正在将用户的语音录制到外部存储器。但是,如果用户没有手机中的SD卡,则应将保存的文件保存到内存中。
static final String PREFIX = "record";
static final String EXTENSION = ".3gpp";
if (file == null) {
File rootDir = Environment.getExternalStorageDirectory();
file = File.createTempFile(PREFIX, EXTENSION, rootDir);
}
如何将同一文件保存到内存?
提前完成!!!!
答案 0 :(得分:0)
您必须先检查外部存储卡的可用性。如果可用,则可以将文件保存在其中。还有手机记忆。
这是您使用内部存储的方式:
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
检查外部存储空间的可用性:
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} 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
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
有关详情,请查看this
答案 1 :(得分:-1)
Try this:
File rootDir = Environment.getExternalStorageDirectory();
rootDir.mkdir();
try {
output=File.createTempFile(PREFIX, EXTENSION, rootDir);
} catch (IOException e1) {
e1.printStackTrace();
}