我试图用下面的代码录制音频。
public void onClick(DialogInterface dialog, int id) {
System.out.println("Inside Audio recording");
File storageDir = new File(Environment.getExternalStorageDirectory()+ "/filetoupload/");
if (!storageDir.exists()) {
storageDir.mkdirs();
}
try {
System.out.println("Inside Audio recording try block");
imageToStore = new File(storageDir,"" + "audio.3gp");
Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(imageToStore));
System.out.println("Inside Audio recording path"+ imageToStore);
startActivityForResult(intent,CAPTURE_AUDIO);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}它将打开默认的android录音播放器,让我记录音频文件。
因此,基本上,它将启动默认的音频记录,并应该在SD卡中创建文件夹名filetoupload,并将其存储为名称audio.3gp,但它不会这样做。它将记录存储在设备中的默认记录文件夹中,或以其他名称(如recording-10555454545.3gp )存储。
所以,我如何能够存储这个音频在SD卡的特定文件夹和与Audio.3gp的名称,请帮助。
发布于 2014-01-31 07:43:18
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == RQS_RECORDING) {
try {
AssetFileDescriptor videoAsset = getContentResolver()
.openAssetFileDescriptor(data.getData(), "r");
FileInputStream fis;
fis = videoAsset.createInputStream();
File root = new File(Environment
.getExternalStorageDirectory().getAbsolutePath() + "/Foldername/", "AUDIO"); // you
if (!root.exists()) {
root.mkdirs();
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
File file;
file = new File(root.getPath() + "/" + "AUD_" + timeStamp
+ ".mp3");
FileOutputStream fos = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while ((len = fis.read(buf)) > 0) {
fos.write(buf, 0, len);
}
fis.close();
fos.close();
Uri fileuri = Uri.fromFile(file);
System.out.println("you Audio path"+fileuri.getpath)
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}https://stackoverflow.com/questions/21474572
复制相似问题