如何访问外部存储文件夹?
我正在尝试从外部存储中获取文件并希望在我的应用程序中显示。如何从外部存储中获取音频文件并在我的应用程序中查看?
这是我的代码:
private String getFilename()
{
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath, AUDIO_RECORDER_FOLDER);
if (!file.exists()) {
file.mkdirs();
}
return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]);
}
private void startRecording() {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(output_formats[currentFormat]);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
//recorder.setOnErrorListener(errorListener);
//recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void stopRecording() {
if (null != recorder) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
}
private View.OnClickListener btnClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1: {
Toast.makeText(call1.this, "Start Recording",
Toast.LENGTH_SHORT).show();
enableButtons(true);
startRecording();
break;
}
case R.id.button2: {
Toast.makeText(call1.this, "Stop Recording", Toast.LENGTH_SHORT)
.show();
enableButtons(false);
stopRecording();
break;
}
case R.id.button3: {
Toast.makeText(call1.this, "Playing Audio", Toast.LENGTH_SHORT)
.show();
}
答案 0 :(得分:0)
你能这样做吗?
File file = new File(getFilesDir()+ "/somefolder");
始终记得在清单文件中添加权限!
这个答案可能会帮助你做得更好;