2013-05-03 9 views
1

Actuellement, j'ai créé l'application Android d'enregistrement audio. Le fichier est enregistré sur le stockage externe.Android enregistrer les fichiers audio dans le stockage interne

Et si je change la méthode pour enregistrer en tant que stockage interne avec le contexte de MODE_PRIVATE

J'ai lu les développeurs Android sur l'enregistrement des fichiers, mais je ne sais pas comment convertir l'instance de fichier dans le tableau d'octets pour sauver le fichier et chargez le fichier audio

le ci-dessous est mon code pour enregistrer le fichier audio

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; 
    } 
} 

le ci-dessous est mon code pour charger le fichier audio

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); 
     } 
    }); 

Répondre

1

Utilisez 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); 

Pour plus voir Using the Internal Storage

+0

comment ouvrir la charge multimédia les fichiers audio si nous sauvons que/données/données/monpaquet au lieu du stockage externe? –

+0

Si vous stockez de l'audio dans un fichier interne, utilisez votre propre lecteur multimédia pour lire l'audio. Consultez http://developer.android.com/guide/topics/media/mediaplayer.html AUTREMENT implémentez un fournisseur de contenu si vous souhaitez le partager avec une autre application. via l'intention –

+0

Je ne comprends pas comment ce code enregistre réellement le fichier à stocker. Quelqu'un pourrait-il expliquer un peu s'il vous plaît? Comment pouvons-nous enregistrer l'audio dans la mémoire et comment l'enregistrer en tant que fichier? – username

Questions connexes