Question

I am recording audio with default audio recorder using Intent, but everytime getting recorded audio in Default location, not in given path, why ?

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

Permissions:-

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Was it helpful?

Solution

Do it like this way,

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

Then create onActivityResult and add below code.

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

and here is your getRealPathFromURI method.

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

Definitely this is gonna work for you.

OTHER TIPS

remove myAudioFile.delete(); this code and try again i think your prob will not coming.

Did you add the proper permissions in your Manifest file?

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
uses-permission android:name="android.permission.STORAGE" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top