【问题标题】:Creating subdirectories within a directory and be able to save data into it在目录中创建子目录并能够将数据保存到其中
【发布时间】:2016-02-13 15:35:25
【问题描述】:

我是 android 开发新手,需要帮助。我有两个问题是:

  1. 我不知道如何在我的主目录中创建两个子目录。我知道如何在目录 (File directory = new File(Environment.getExternalStorageDirectory()+"Saling-Wika/Audio&Text Files");) 中创建子目录,但我想要的是我的主子目录中有两个子目录(音频文件和文本文件是 Saling-Wika 目录中不同的两个子目录是主目录)。
  2. 我不知道如何将我的数据保存到我创建的子目录中。 这是音频数据来自的记录模块的代码:

    public class RecordModule extends Activity {
    
    Button SpeakBtn, StopBtn;
    private MediaRecorder myAudioRecorder;
    private String outputFile = null;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recordmodule);
    
        SpeakBtn = (Button) findViewById(R.id.SpeakBtn);
        StopBtn = (Button) findViewById(R.id.StopBtn);
    
        StopBtn.setEnabled(false);
        SpeakBtn.setEnabled(true);
        SimpleDateFormat datetime = new SimpleDateFormat("ddMMyyyyhhmmss");
        String format = datetime.format(new Date());
        outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + format + ".3gp";
    
        myAudioRecorder = new MediaRecorder();
        myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        myAudioRecorder.setOutputFile(outputFile);
    
        SpeakBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    myAudioRecorder.prepare();
                    myAudioRecorder.start();
                }
    
                catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
                catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
                SpeakBtn.setEnabled(false);
                StopBtn.setEnabled(true);
    
                Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_SHORT).show();
            }
        });
    
        StopBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myAudioRecorder.stop();
                myAudioRecorder.release();
                myAudioRecorder = null;
    
                StopBtn.setEnabled(false);
                SpeakBtn.setEnabled(true);
    
                Toast.makeText(getApplicationContext(), "Audio recorded successfully", Toast.LENGTH_SHORT).show();
            }
        });
    }
    

我希望将我的音频数据存储到 Audio Files 目录中,但我不知道如何才能做到这一点。请帮帮我。

【问题讨论】:

  • 单独制作你的子目录。你的问题太奇怪了。如果您可以创建一个子目录,那么您可以创建另一个我认为的子目录。
  • 我终于分别做了子目录,第一个问题解决了。第二个怎么样?你能帮我吗? @greenapps
  • 好吧,看来您是命令 myAudioRecorder 来做的。真的吗?那你为什么不这么说?你也忘了告诉现在发生了什么。你在哪里给你的录音机指明正确的目录?
  • 我很困惑,@greenapps。是的 myAudioRecorder 做到了。我在哪里向我的录音机指示正确的目录是什么意思?

标签: android audio directory saving-data


【解决方案1】:

我终于解决了我的问题,以下是我所做的解决方案:

String folder_main = "Saling-Wika"

File MainDir = new File (Environment.getExternalStorage();, folder_main);
if (!MainDir.exists()){
   MainDir.mkdirs();
}

File f1 = new File (Environment.getExternalStorage() + "/" + folder_main, "Audio Files");
if (!MainDir.exists()){
   MainDir.mkdirs();
}

File f2 = new File (Environment.getExternalStorage() + "/" + folder_main, "Text Files");
if (!MainDir.exists()){
   MainDir.mkdirs();
}
}

这是我如何在主目录 (Saling-Wika fodler) 中创建两个子目录 (Audio & Text Files 文件夹) 的代码。

outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Saling-Wika/Audio Files/" + format + ".3gp";

以下是我的音频文件如何保存到 Audio Files 文件夹中。

【讨论】:

    猜你喜欢
    • 2023-01-18
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 2012-10-01
    • 2021-12-11
    • 1970-01-01
    • 2020-04-21
    相关资源
    最近更新 更多