繁体   English   中英

将音频保存到 SD 卡

[英]Save Audio to SD CARD

我一直在寻找将音频保存到 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 卡位置,

    环境.getExternalStorageDirectory();

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM