Android Get Bucket Id of Camera Album

Aug 31, 2020

Solution 1

fun getCameraBucketId(): Int {    val bucketPath = Environment.getExternalStoragePublicDirectory("/DCIM/Camera").toString()    return bucketPath.toLowerCase().hashCode().toLong()}

NOTE: Environment.getExternalStoragePublicDirectory is deprecated in API level 29

Solution 2

Search for all albums with the name "Camera".

fun getCameraBucketId(): Long? {  for (item in findAlbums()) {    if (item.name == "Camera") {        cameraBucketId = item.id        return item.id    }  }  return null}

NOTE: Refer to findAlbums

Solution 3

Combine solution 1 and 2

private var cameraBucketId: Long? = null@Suppress("DEPRECATION")fun getCameraBucketId(): Long? {    if (cameraBucketId == null) {        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {            val bucketPath = Environment.getExternalStoragePublicDirectory("/DCIM/Camera").toString()            cameraBucketId = bucketPath.toLowerCase().hashCode().toLong()            return cameraBucketId        }        else {                            for (item in findAlbums()) {                    Timber.d("${item.name}=${item.count}, ${item.id}")                    if (item.name == "Camera") {                        cameraBucketId = item.id                        return cameraBucketId                    }                }            }                    }    return cameraBucketId}

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.