0

我正在制作一个应用程序,只要触摸并按住图像按钮就可以录制声音,但是在完成录制后我无法访问文件。

这是输出文件的代码:

 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。

此外,这是完整的 mainactivity 代码,如果问题可能出在其他地方:

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
4

1 回答 1

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。如果您的应用程序被卸载,后者将允许您的文件保留,前者不会。

于 2013-07-31T03:12:23.320 回答