gpt4 book ai didi

android - 为什么只在默认位置获取录制的音频

转载 作者:行者123 更新时间:2023-11-30 03:07:03 26 4
gpt4 key购买 nike

我正在使用 Intent 使用默认录音机录制音频,但每次都在默认位置而不是在给定路径中录制音频,为什么?

File dir = new File
(Environment.getExternalStorageDirectory().getAbsolutePath() +"/Ringtone");
dir.mkdirs();
myAudioFile = new File(dir+"/RecordedAudio.3gpp");
myAudio = Uri.fromFile(myAudioFile);
Intent i = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
i.putExtra(MediaStore.EXTRA_OUTPUT, myAudio);
startActivity(i);

权限:-

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />

最佳答案

这样做,

Intent i = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
i.putExtra(MediaStore.EXTRA_OUTPUT, myAudio);

然后创建onActivityResult 并添加下面的代码。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode,
resultCode, data);
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case CAPTURE_AUDIO:

Uri audioUri = data.getData();
String absolutepath = getRealPathFromURI(audioUri);

String path2 = Environment.getExternalStorageDirectory()
+ "/Ringtone/";
File file = new File(absolutepath);
File file2 = new File(path2 + "RecordedAudio.3gpp");
file.renameTo(file2);
break;

default:
break;
}
}
}

这是您的 getRealPathFromURI 方法。

public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Audio.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
System.out.println("absolutepath audiopath in getRealPathFromURI : "
+ cursor.getString(column_index));
return cursor.getString(column_index);
}

这绝对适合您。

关于android - 为什么只在默认位置获取录制的音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21625889/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com