如何在特定文件夹中创建文件?

时间:2017-03-04 14:37:16

标签: android file directory

我刚创建了一个录音应用程序,当它创建新的音频文件时,我希望它在特定的文件夹中(这里是示例

这就是我制作文件的方式

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

这是我为新文件创建的文件夹

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

每当应用程序创建新文件时,它都应自动转到 sample 文件夹

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

3 个答案:

答案 0 :(得分: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来创建文件。

答案 1 :(得分:0)

由于sample是默认目录,

 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;

答案 2 :(得分: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;