为什么仅在默认位置获取录制的音频

时间:2014-02-07 10:55:36

标签: android audio android-sdcard

我使用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" />

3 个答案:

答案 0 :(得分:1)

这样做,

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);
    }

这肯定会对你有用。

答案 1 :(得分:0)

删除myAudioFile.delete();此代码,然后再试一次,我认为您的问题不会出现。

答案 2 :(得分:0)

您是否在Manifest文件中添加了适当的权限?

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