微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将音频保存到SD卡

如何解决将音频保存到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"/>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?