1

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" />
Sun
  • 6,768
  • 25
  • 76
  • 131
  • 1
    Because you are deleting directory `myAudioFile.delete();` remove this line and you are done. – InnocentKiller Feb 07 '14 at 10:57
  • removed but still not resolved – Sun Feb 07 '14 at 11:00
  • Because you are recording audio with default audio recorder that is With Intent it will store recorded audio to it's default directory only no matter what path you give. The solution is you have to get the path of last recorded audio and then you have to move or copy that file to your destination. – InnocentKiller Feb 07 '14 at 11:02
  • I too was having same problem few days back and i have done with above way only. – InnocentKiller Feb 07 '14 at 11:04
  • @InnocentKiller yeah same kind of issue i am facing, is there any way to resolve this ? and how to get path of last recorded audio ? – Sun Feb 07 '14 at 11:05
  • is there any way to resolve this without getting path of last recorded audio ? – Sun Feb 07 '14 at 11:13
  • No, because different device has a different path to store, so you can't do it with any other way. But still i don't know much if you find any best way then this, then update me too. But this is perfectly working for me. and why don't you want to use it by this way, what's the problem??? – InnocentKiller Feb 07 '14 at 11:15
  • Whether it worked or not??? – InnocentKiller Feb 07 '14 at 11:22

3 Answers3

1

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.

InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
  • it means no hope for developer defined path :( – Sun Feb 07 '14 at 11:22
  • last question, can i store video in my own location (like: IMAGES) or not (like: Ringtones) ? – Sun Feb 07 '14 at 11:26
  • Yes that you can do... Same as u did in audios. No need to do do it in this way. – InnocentKiller Feb 07 '14 at 11:27
  • bro :) thanks tried and done with videos... but having small doubt how to attach both files Captured Image and Recorded Video in a mail, i know for a single file, but don't know about multiple, can you show required code? – Sun Feb 07 '14 at 11:35
  • For this you can refer this link http://stackoverflow.com/questions/2264622/android-multiple-email-attachments-using-intent or http://stackoverflow.com/questions/4552831/how-to-attach-multiple-files-to-email-client-in-android – InnocentKiller Feb 07 '14 at 11:39
0

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

Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49
0

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" />
Ana
  • 166
  • 1
  • 16