zx6r
zx6r

Reputation: 99

android: get media folder (for videos)

Is there an API call on Android to get the path to the folder where the Camera app stores the recorded videos? I'm currently using this snippet but was wondering if there's a proper (cleaner) way to do this?

// check file path exists!
        File f = new File(Environment.getExternalStorageDirectory() + "/Videos");
        if (!f.isDirectory()) {
            f = new File(Environment.getExternalStorageDirectory() + "/DCIM");
            if (!f.isDirectory()) {
                f = new File(Environment.getExternalStorageDirectory() + "/");
            }
        }
        String path = f.getAbsolutePath().toString();

EDIT:

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)

returns

/storage/emulated/0/DCIM

But,

| => adb shell
shell@d2can:/ $ cd /storage/emulated
shell@d2can:/storage/emulated $ ls
legacy
shell@d2can:/storage/emulated $ cd 0
/system/bin/sh: cd: /storage/emulated/0: No such file or directory
2|shell@d2can:/storage/emulated $ 

Bizarrely enough, if I actually go ahead and try to create files at that non-existent location (from within the android app), they do get created in /sdcard/DCIM !! Yet when inspecting the app variables with the debugger, they have the path as shown above - the invalid one.

Upvotes: 4

Views: 5340

Answers (2)

CodeToLife
CodeToLife

Reputation: 4171

  Environment.getExternalStoragePublicDirectory(Environment.
DIRECTORY_DCIM).getAbsolutePath();

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007564

Is there an API call on Android to get the path to the folder where the Camera app stores the recorded videos?

There is no one "Camera app". There are thousands of Android device models. Dozens, if not hundreds, of camera apps are pre-installed across those device models. There are hundreds, if not thousands, of other camera apps available for users to download from the Play Store or elsewhere.

None have to save recorded videos in any specific spot, let alone a spot that you can get to via Java file I/O.

I'm currently using this snippet but was wondering if there's a proper (cleaner) way to do this?

You can get the locations where the device recommends that files be stored on external storage via Environment.getExternalStoragePublicDirectory(). Specifically, DIRECTORY_DCIM is described as "The traditional location for pictures and videos when mounting the device as a camera".

However:

  • Camera apps are not required to use this directory

  • Camera apps that do pay attention to DIRECTORY_DCIM often create an app-specific subdirectory

Upvotes: 2

Related Questions