2013-05-03 134 views
1

目前我已創建了音頻錄製Android應用程序。該文件保存在外部存儲器中。Android將音頻文件保存到內部存儲

如果我改變方法爲內部存儲保存MODE_PRIVATE

的背景下

我看了一下保存文件的Android開發,但我不知道如何將文件實例轉換成字節數組保存該文件並加載音頻文件

下面是我的代碼保存音頻文件

public void onClick(View view) throws Exception { 
    if (count == 0) { 

     tbxRecordStatus.setText("Record"); 
     btnRecord.setText("Stop Record"); 

     Toast.makeText(MainActivity.this, "Recording Starts", 
       Toast.LENGTH_SHORT).show(); 
     dateInString = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(
       new Date()).toString(); 
     String fileName = "TVSSSSD_" + dateInString + " record.3gp"; 

     SDCardpath = Environment.getExternalStorageDirectory(); 
     myDataPath = new File(SDCardpath.getAbsolutePath() 
       + "/.My Recordings"); 

     // mydir = context.getDir("media", Context.MODE_PRIVATE); 
     if (!myDataPath.exists()) 
      myDataPath.mkdir(); 


     audiofile = new File(myDataPath + "/" + fileName); 

     Log.d("path", myDataPath.getAbsolutePath()); 
     // File fileWithinMyDir = new File(mydir, fileName); 
     // audiofile = fileWithinMyDir; 
     // FileOutputStream out = new FileOutputStream(fileWithinMyDir); 

     recorder = new MediaRecorder(); 
     recorder.reset(); 
     recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
     recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
     recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
     recorder.setAudioEncodingBitRate(16); 
     recorder.setAudioSamplingRate(44100); 

     recorder.setOutputFile(audiofile.getAbsolutePath()); 

     recorder.prepare(); 

     recorder.start(); 

     count++; 

    } else { 

     tbxRecordStatus.setText("Stop"); 
     btnRecord.setText("Start Record"); 
     Toast.makeText(MainActivity.this, "Recording Stops", 
       Toast.LENGTH_SHORT).show(); 
     if (recorder != null) { 
      recorder.stop(); 
      recorder.release(); 

      recorder = null; 

     } else { 
      tbxRecordStatus.setText("Warning!"); 
      Toast.makeText(MainActivity.this, "Record First", 
        Toast.LENGTH_SHORT).show(); 
     } 
     count = 0; 
    } 
} 

下面是我的代碼加載音頻文件

recordList = new ArrayList<String>(); 
    File directory = Environment.getExternalStorageDirectory(); 
    file = new File(directory + "/My Recordings"); 
    File list[] = file.listFiles(); 
    for (int i = 0; i < list.length; i++) { 
     recordList.add(list[i].getName()); 
    } 

    Collections.sort(recordList, Collections.reverseOrder()); 

    listview = (ListView) findViewById(R.id.listView); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, android.R.id.text1, 
      recordList); 


    listview.setAdapter(adapter); 

    listview.setTextFilterEnabled(true); 

    listview.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
       long arg3) { 
      // TODO Auto-generated method stub 


      String product = ((TextView) view).getText().toString(); 
      String fullPathAudio = file.getAbsolutePath() + "/" + product; 

      File resultFile = new File(fullPathAudio); 

      Intent intent = new Intent(); 
      intent.setAction(android.content.Intent.ACTION_VIEW); 
      intent.setDataAndType(Uri.fromFile(resultFile), "audio/*"); 
      startActivity(intent); 
     } 
    }); 

回答

1

使用getFilesDir()

SDCardpath = getFilesDir(); 
     myDataPath = new File(SDCardpath.getAbsolutePath() 
       + "/.My Recordings"); 

     // mydir = context.getDir("media", Context.MODE_PRIVATE); 
     if (!myDataPath.exists()) 
      myDataPath.mkdir(); 


     audiofile = new File(myDataPath + "/" + fileName); 

更多看到Using the Internal Storage

+0

如何打開介質裝入背面的音頻文件,如果我們保存,作爲/數據/數據/ mypackage的,而不是外部存儲? – 2013-05-03 06:27:42

+0

如果您將音頻存儲在內部文件中,請使用自己的媒體播放器播放音頻。請參閱http://developer.android.com/guide/topics/media/mediaplayer.html如果您想與其他應用程序共享,請實施內容提供商通過意圖 – 2013-05-03 06:33:01

+0

我不明白這個代碼實際上是如何_saves_文件存儲。有人可以解釋一下嗎?我們如何將音頻存入內存以及如何將其保存爲文件? – username 2016-01-01 02:40:05

相關問題