devashish chourey devashish chourey - 1 year ago 70
Android Question

how to save audio files in specific folder in sd card(at present files are saving directly in sdcard)

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;
import java.util.Date;
import cafe.adriel.androidaudiorecorder.AndroidAudioRecorder;
import cafe.adriel.androidaudiorecorder.Util;
import cafe.adriel.androidaudiorecorder.example.R;

public class MainActivity extends AppCompatActivity
public static final String EXTRA_FILE_PATH = "filePath";

public static final String EXTRA_COLOR = "color";
static Date createdTime= new Date();
public static final String AUDIO_FILE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + createdTime + "_rec.wav";
public static final int RECORD_AUDIO = 0;

protected void onCreate(Bundle savedInstanceState) {

if (getSupportActionBar() != null) {
new ColorDrawable(getResources().getColor(R.color.colorPrimaryDark)));

Util.requestPermission(this, Manifest.permission.RECORD_AUDIO);
Util.requestPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RECORD_AUDIO) {
if (resultCode == RESULT_OK) {
Toast.makeText(this, "Audio recorded successfully!", Toast.LENGTH_SHORT).show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Audio was not recorded", Toast.LENGTH_SHORT).show();

public void recordAudio(View v) {

Answer Source

Consider using one Android's default external storage folders, in your case the DIRECTORY_MUSIC like this

File path = Environment.getExternalStoragePublicDirectory(

You can use this File class constructor to create a new folder on the SD card:

File audioFolder = new File(Environment.getExternalStorageDirectory(), 
if (!audioFolder.exists()) {
    boolean success = audioFolder.mkdir()
    if (success) {
        // save the file 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download