Search code examples
androidvideostorageinternal

How do I record video to internal storage?


I ran into a problem when my test phone recorded video and did not have a external sd-card. i get recording errors because:

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)

points to the external sd-card and since the external sd does not exist, the phone is not smart enough to point to the internal storage.

  1. Is there a solution to this issue?

  2. If not, how do I record directly to the internal storage?


Solution

  • If you want to test whether an SD card exists or not, read the documentation on Environment.getExternalStorageState(). This includes code that demonstrates how to test if the SD card is mounted, readable and / or writable. It even registers a BroadcastReceiver should the card be removed while your app in running.

    From this you can divert the saved file to internal storage whenever the SD card is not viable.

    Addition from comment

    You don't provide any detail on how you are currently testing the availability of the SD card, so I've generalized the process.

    MediaRecorder mediaRecorder = new MediaRecorder();
    ...
    
    String state = Environment.getExternalStorageState();
    if(Environment.MEDIA_MOUNTED.equals(state))
        mediaRecorder.setOutputFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) + "/video.mp4");
    else
        mediaRecorder.setOutputFile(getFilesDir().getAbsolutePath() + "/video.mp4");