devashish chourey devashish chourey - 3 months ago 10
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.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
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;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

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

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

@Override
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) {
AndroidAudioRecorder.with(this)
.setFilePath(AUDIO_FILE_PATH)
.setColor(getResources().getColor(R.color.recorder_bg))
.setRequestCode(RECORD_AUDIO)
.record();
}
}

Answer

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

File path = Environment.getExternalStoragePublicDirectory(
    Environment.DIRECTORY_MUSIC);

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

File audioFolder = new File(Environment.getExternalStorageDirectory(), 
    "newaudiofolder")
if (!audioFolder.exists()) {
    boolean success = audioFolder.mkdir()
    if (success) {
        // save the file 
    }
}