使用MediaRecorder停止录制后找不到文件目录

时间:2013-07-31 00:03:33

标签: java android eclipse android-mediarecorder

我正在创建一个记录声音的应用程序,只要触摸并保持图像按钮,但在录制完成后我无法访问该文件。

这是输出文件的代码:

 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
    {
        externalStoragePath = getExternalCacheDir().getAbsolutePath();
        recorder.setOutputFile(externalStoragePath + "/easyvoicenotepad/test.3gp");
    }
    else
    {
        storagePath = Environment.getDataDirectory().getAbsolutePath();
        recorder.setOutputFile(storagePath + "/easyvoicenotepad/test.3gp");
    }

我记录了它,结果就是这样:

07-31 01:49:54.582: I/storage(332): File saved to: /storage/emulated/0/Android/data/com.whizzappseasyvoicenotepad/cache/easyvoiceno‌​tepad/test.3gp

我在设备上的任何地方找不到这个目录(我甚至不使用模拟器),如果有帮助,我的设备就是三星Galaxy Nexus。

此外,这里是完整的主要活动代码,如果问题可能在其他地方:

public class MainActivity extends Activity {

MediaRecorder recorder;
String storagePath;
String externalStoragePath;

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

    //AUDIO RECORDER
    recorder = new MediaRecorder();
    recorder.reset();       
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
    {
        externalStoragePath = getExternalCacheDir().getAbsolutePath();
        recorder.setOutputFile(externalStoragePath + "/easyvoicenotepad/test.3gp");
    }
    else
    {
        storagePath = Environment.getDataDirectory().getAbsolutePath();
        recorder.setOutputFile(storagePath + "/easyvoicenotepad/test.3gp");
    }

    //IMAGE BUTTON ONTOUCHLISTENER
    final ImageButton recBtn = (ImageButton) findViewById(R.id.recButton);
    recBtn.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN)
            {
                recBtn.setImageResource(R.drawable.record_btn_pressed);
                try {
                    recorder.prepare();
                    recorder.start();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            else if (event.getAction() == MotionEvent.ACTION_UP)
            {
                recBtn.setImageResource(R.drawable.record_btn);
                try {
                    recorder.prepare();
                    recorder.stop();
                    recorder.reset();
                    recorder.release();
                    recorder = null;
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Log.i(STORAGE_SERVICE, "File saved to: " + externalStoragePath + "/easyvoicenotepad/test.3gp");
            }
            return true;
        }

    });
} //END OF ONCREATE

1 个答案:

答案 0 :(得分:0)

Context.getExternalCacheDir适用于临时文件。

根据API:

External files are not always available: they will disappear if the user mounts the external storage on a computer or removes it.

对于存储在Context.getExternalFilesDir

的文件也是如此

所以你可能想要Context.getFilesDirEnvironment.getExternalStoragePublicDirectory。如果你的应用程序被卸载,后者将允许你的文件保持不变,前者不会。