简体   繁体   中英

Android I want to access my external storage folder

How can I access my External storage folder?

I am trying to fetch the files from external storage and that want to be display in my application. How can i get an audio file from external storage and view it in my application?

Here's my code:

private String getFilename()
{

    String filepath = Environment.getExternalStorageDirectory().getPath();

    File file = new File(filepath, AUDIO_RECORDER_FOLDER);

    if (!file.exists()) {
        file.mkdirs();
    }

    return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]);
}
private void startRecording() {
    recorder = new MediaRecorder();

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(output_formats[currentFormat]);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(getFilename());

    //recorder.setOnErrorListener(errorListener);
    //recorder.setOnInfoListener(infoListener);

    try {
        recorder.prepare();
        recorder.start();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void stopRecording() {
    if (null != recorder) {
        recorder.stop();
        recorder.reset();
        recorder.release();
        recorder = null;
    }
}



private View.OnClickListener btnClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1: {
            Toast.makeText(call1.this, "Start Recording",
                    Toast.LENGTH_SHORT).show();

            enableButtons(true);
            startRecording();

            break;
        }
        case R.id.button2: {
            Toast.makeText(call1.this, "Stop Recording", Toast.LENGTH_SHORT)
            .show();
            enableButtons(false);
            stopRecording();

            break;
        }

        case R.id.button3: { 
            Toast.makeText(call1.this, "Playing Audio", Toast.LENGTH_SHORT)
            .show();
}

Can you do this?

File file = new File(getFilesDir()+ "/somefolder");

Always remember to get the permissions added in the manifest file!

This answer might help you even better;

How to access external storage in android

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM