Question

I am trying to record audio with the following code.

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

}
}

It will open default android recording player and let me record audio file.

So basically it will start default audio recording and should create folder name filetoupload inside SD-card and should store it is as a name audio.3gp but it is not doing same. It is storing recording inside default recording folder in device or with some other name like recording-10555454545.3gp

So how can i store this audio inside SD-card particular folder and with audio.3gp name please help.

Was it helpful?

Solution

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

            }


}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top