2013-05-03 184 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

相关问题