2

Recently I'm working with a camera app where I need insert all the images I'm doing in the MediaStore. I saw that there is two ways for it, but after read the documentation I don't see any clear difference between both of them.

Using MediaStore

return Images.Media.insertImage(app.contentResolver, metadata.path, metadata.name, "")

Using ContentResolver

 val contentValues = ContentValues().apply {
        put(Images.Media.DATA, metadata.path)
        put(Images.Media.MIME_TYPE, appImg.mimeType)
        put(Images.Media.DISPLAY_NAME, metadata.name)
        put(Images.Media.DATE_TAKEN, currentTime)
        put(Images.Media.DATE_ADDED, currentTime)
        put(Images.ImageColumns.SIZE, appImg.sizeInBytes)
        put(Images.Media.WIDTH, appImg.width)
        put(Images.Media.HEIGHT, appImg.height)
        appImg[MediaMetadata.Location]?.let {
            put(Images.Media.LATITUDE, it.latitude)
            put(Images.Media.LONGITUDE, it.longitude)
        }
    }

    return app.contentResolver.insert(Images.Media.EXTERNAL_CONTENT_URI, contentValues)

Reading some doc I saw that the second one is also creating a thumbnail, but it doesn't explain if it creates it forever, so this will be store in the MediaStore as well and I will be able to query and retrieve the Thumbnails faster.

Anyone have work with both versions and can explain when to use one or the other?

halfer
  • 19,824
  • 17
  • 99
  • 186
Francisco Durdin Garcia
  • 12,540
  • 9
  • 53
  • 95

2 Answers2

2

Although from the MediaStore source code it seems that insertImage() is equal to using contentResolver.insert() with the right values but is also marked as deprecated. So you probably should use the latter.

Also be aware that starting with Android Q (API 29) location data is no longer stored in the MediaStore database (it's stored in Exif meta data). See the media storage guide for more info on Android Q. Also any DATA field will be ignored if you don't have legacy storage enabled or are a system app.

CodeRed
  • 481
  • 4
  • 17
  • Hi @CodeRed, I have an issue with a specific case. Would be great if you could find time to check: https://stackoverflow.com/questions/66148310/storing-images-using-mediastore-crashes-app-on-specific-samsung-devices – SwissMark Feb 12 '21 at 08:45
0

Just to add on top of what @CodeRed has reply above, same sample code at https://github.com/android/storage-samples is really helpful. In the sample code, I don't see any MediaStore code and the link above given by @CodeRed is no longer accessible.

hjchin
  • 864
  • 2
  • 8
  • 25