2017-03-04 47 views
0

我剛剛創建了一個錄音應用程序,當它創造了新的音頻文件,我想這是有特定文件夾(在這裏樣品如何使文件在特定文件夾中?

這是怎麼了我想提出的文件

outputfile= Environment.getExternalStorageDirectory().getAbsolutePath()+"/recording_"+System.currentTimeMillis()+".mp3"; 

,這是我爲新創建的文件夾的文件

File directory=new File(Environment.getExternalStorageDirectory()+separator+"sample"); 
    directory.mkdirs(); 

每當應用程序創建新文件,它會自動轉到樣品文件夾

我是android的初學者,那些誰可以幫助我,請提供代碼也感謝

這裏是我的主要活動

package com.hackerinside.jaisonjoseph.sample_recorder; 

import android.media.MediaPlayer; 
import android.media.MediaRecorder; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Button; 
import android.widget.Toast; 

    import java.io.File; 
    import java.io.IOException; 

    import static java.io.File.separator; 

    public class MainActivity extends AppCompatActivity { 

private Button play,stop,record; 
private MediaRecorder myAudioRecorder; 
private String outputfile; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    play=(Button)findViewById(R.id.play); 
    stop=(Button)findViewById(R.id.stop); 
    record=(Button)findViewById(R.id.record); 
    stop.setEnabled(false); 
    play.setEnabled(false); 




    File directory=new File(Environment.getExternalStorageDirectory()+separator+"jaison"); 
    // directory.mkdirs(); 


    outputfile= Environment.getExternalStorageDirectory().getAbsolutePath()+"/recording_"+System.currentTimeMillis()+".mp3"; 





    record.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 

     try { 


      myAudioRecorder=new MediaRecorder(); 
      myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
      myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); 
      myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.DEFAULT); 
      myAudioRecorder.setOutputFile(outputfile); 
      myAudioRecorder.prepare(); 
      myAudioRecorder.start(); 
    } 
    catch (IllegalStateException ise){ 



    }catch (IOException ioe){ 



     } 
      record.setEnabled(false); 
      stop.setEnabled(true); 
      Toast.makeText(getApplicationContext(),"record startded",Toast.LENGTH_LONG).show(); 

    } 

}); 





    stop.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 



      myAudioRecorder.stop(); 
      record.setEnabled(true); 
      myAudioRecorder.release(); 
      myAudioRecorder=null; 
      stop.setEnabled(false); 
      play.setEnabled(true); 
      Toast.makeText(getApplicationContext(),"recorded audio",Toast.LENGTH_LONG).show(); 

     } 

    }); 
      play.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        MediaPlayer mediaPlayer=new MediaPlayer(); 

        try { 
          mediaPlayer.setDataSource(outputfile); 
          mediaPlayer.prepare(); 
          mediaPlayer.start(); 
          Toast.makeText(getApplicationContext(),"playing audio",Toast.LENGTH_LONG).show(); 

    } 
    catch (Exception e) 
    { 

    } 

    } 
}); 








    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 











} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 

回答

0
File directory=new File(Environment.getExternalStorageDirectory(),"sample"); 
if(!directory.exists()) 
    { 
    if(!directory.mkdirs()) 
     { 
     Toast (could not create directory: + directory.getAbsolutePath()); 
     return; 
     } 
    } 

File file = new File(directory, "recording_"+System.currentTimeMillis()+".mp3"); 

此代碼不會創建一份文件。只有一個File對象。但是您可以使用File對象file來創建文件。

+0

OUTPUTFILE是一個變量,所以我不力看到任何變量名OUTPUTFILE在你的代碼 –

+0

增加了截圖,現在可以用代碼 –

+0

來幫助你,請刪除截圖。代碼應作爲文本發佈。 – greenapps

0

由於樣本是默認目錄,

String fileName = "/recording_" + System.currentTimeMillis() + ".mp3"; 
final private String directory = Environment.getExternalStorageDirectory()+ "/sample"; 

創建目錄,如果不存在。

if(!new File(directory).exists()){ 
    if(!file.mkdir()){/*error*/ } 
} 

現在,新的文件路徑

String outputfile = directory + "/" + fileName; 
+0

增加了截圖,現在可以用代碼 –

+0

代替'play.setEnabled(false)'後面的3行代碼,用我上面的5行代碼。 – samirk433

+0

由於您不檢查mkdir的返回值,因此代碼不正確。 – greenapps

0

我得到了答案謝謝大家,幫助

File directorysample=new File(Environment.getExternalStorageDirectory()+separator+"sample"); 
    directorysample.mkdirs(); 
    String filename = "/recording_" + System.currentTimeMillis() + ".mp3"; 
    final String directory = Environment.getExternalStorageDirectory()+ "/sample"; 
    outputfile = directory + "/" + filename; 
相關問題