如何将音频文件保存在sd卡的特定文件夹中(目前文件都是直接保存在sdcard中)
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();
}
}
考虑像这样使用一个 Android's default external storage folders, in your case the DIRECTORY_MUSIC
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MUSIC);
您可以使用此 File class constructor 在 SD 卡上创建一个新文件夹:
File audioFolder = new File(Environment.getExternalStorageDirectory(),
"newaudiofolder")
if (!audioFolder.exists()) {
boolean success = audioFolder.mkdir()
if (success) {
// save the file
}
}
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();
}
}
考虑像这样使用一个 Android's default external storage folders, in your case the DIRECTORY_MUSIC
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MUSIC);
您可以使用此 File class constructor 在 SD 卡上创建一个新文件夹:
File audioFolder = new File(Environment.getExternalStorageDirectory(),
"newaudiofolder")
if (!audioFolder.exists()) {
boolean success = audioFolder.mkdir()
if (success) {
// save the file
}
}