将音频保存到SD卡



我一直在寻找将音频保存到SdCard。

File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + "/AUDIO/");
boolean success1;
if (!dir.exists()) {
MessageSystem("SAVING FOLDER: " + dir + " -- DON'T EXISTS ");
//CASE NOT -  We create
success1 = dir.mkdir();

if (success1) {
// Do something on success
MessageSystem("foldersd: " + dir.toString() +  " CREATED!!");

} else {
// Do something else on failure
MessageSystem("foldersd: " + dir.toString() +  " NOT CREATED!!");
}
}

我也尝试过保存到:


File dir = new File(Environment.getExternalStorageDirectory() + "/AUDIO/");

在这两种情况下,它都起作用,但它保存到内部存储。从不使用SD卡

我使用了getExternalFilesDirs((。但如果用户卸载应用程序,就会丢失文件。

我不想那样。

如何将音频保存在SD卡公用文件夹中或创建新的音频。

它创建到内部存储的任何时间。

有解决方案吗???

我尝试过这个,我可以在SD卡中创建一个文档。还有空的。现在我想将MediaRecorder音频保存到其中。但我需要路径。我怎样才能拿到它?

Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/mpeg");
intent.putExtra(Intent.EXTRA_TITLE, audio_name);
// Optionally, specify a URI for the directory that should be opened in
// the system file picker when your app creates the document.
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri);
startActivityForResult(intent, CREATE_FILE);

你可以试试这个,

public static final String DEFAULT_STORAGE_LOCATION = "/sdcard/AudioRecording";
File dir = new File(DEFAULT_STORAGE_LOCATION);
if (!dir.exists()) {
try {
dir.mkdirs();
} catch (Exception e) {
Log.e("CallRecorder", "RecordService::makeOutputFile unable to create directory " + dir + ": " + e);
Toast t = Toast.makeText(getApplicationContext(), "CallRecorder was unable to create the directory " + dir + " to store recordings: " + e, Toast.LENGTH_LONG);
t.show();
return null;
}
} else {
if (!dir.canWrite()) {
Log.e(TAG, "RecordService::makeOutputFile does not have write permission for directory: " + dir);
Toast t = Toast.makeText(getApplicationContext(), "CallRecorder does not have write permission for the directory directory " + dir + " to store recordings", Toast.LENGTH_LONG);
t.show();
return null;
}
  1. 您可以通过查询系统获得sd卡位置,如

    Environment.getExternalStorageDirectory((;

并且不要忘记添加用户权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

最新更新