2016-02-13 28 views
0

我是新來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(); 
         } 
        }); 
    } 
    

我想我的音頻數據將被存儲到音頻文件目錄,但我不知道我怎麼樣米將能夠做到這一點。請幫幫我。

+0

那麼分開製作你的子目錄。你的問題很奇怪。如果你可以創建一個子目錄,那麼你可以創建另一個我會想到的。 – greenapps

+0

我終於分開製作子目錄,所以第一個問題就解決了。第二個呢?你能幫我嗎? @greenapps –

+0

看起來你已經訂購了myAudioRecorder來做到這一點。真的嗎?那你爲什麼不這麼說呢?你也忘了告訴現在發生了什麼。你在哪裏指出你的錄音機的正確目錄? – greenapps

回答

0

我終於解決了我的問題,這裏是我所做的解決方案:

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)。

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

這裏是我的音頻文件如何保存到音頻文件文件夾。

相關問題