简体   繁体   English

Android MediaStore:Images.Media.insertImage 与 ContentResolver.insert

[英]Android MediaStore : Images.Media.insertImage vs ContentResolver.insert

Recently I'm working with a camera app where I need insert all the images I'm doing in the MediaStore.最近我正在使用一个相机应用程序,我需要在其中插入我在 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使用 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.阅读一些文档我看到第二个也在创建缩略图,但它没有解释它是否永远创建它,所以这也将存储在 MediaStore 中,我将能够更快地查询和检索缩略图。

Anyone have work with both versions and can explain when to use one or the other?任何人都使用过这两个版本并且可以解释何时使用其中一个?

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.尽管从MediaStore源代码看来insertImage()等同于使用具有正确值的contentResolver.insert()但也被标记为已弃用。 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).另请注意,从 Android Q (API 29) 开始,位置数据不再存储在 MediaStore 数据库中(存储在 Exif 元数据中)。 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.有关 Android Q 的更多信息,请参阅媒体存储指南。此外,如果您没有启用旧存储或系统应用程序,则任何DATA字段都将被忽略。

Just to add on top of what @CodeRed has reply above, same sample code at https://github.com/android/storage-samples is really helpful.除了上面@CodeRed 的回复之外, https ://github.com/android/storage-samples 上的相同示例代码真的很有帮助。 In the sample code, I don't see any MediaStore code and the link above given by @CodeRed is no longer accessible.在示例代码中,我没有看到任何 MediaStore 代码,并且@CodeRed 上面给出的链接不再可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android-Images.Media.insertImage会将alpha为0的每个像素都变为黑色 - Android - Images.Media.insertImage is turning every pixel with alpha of 0 to black android在Images.Media.insertImage之后删除文件 - android delete file after Images.Media.insertImage 为什么 Images.Media.insertImage 返回 null - Why Images.Media.insertImage return null MediaStore contentResolver.insert() 在拍照时创建副本而不是替换现有文件(Android Q:29) - MediaStore contentResolver.insert() creates copies instead of replacing the existing file when taking photos (Android Q: 29) MediaStore.Images.Media.insertImage - java.io.FileNotFoundException - Android - MediaStore.Images.Media.insertImage - java.io.FileNotFoundException - Android android.provider.MediaStore.Images.Media.insertImage返回null - android.provider.MediaStore.Images.Media.insertImage returns null Android - MediaStore.Images.Media.insertImage - 无法创建文件 - Android - MediaStore.Images.Media.insertImage - Unable to create file Android ContentResolver.insert() 抛出 NullPointerException - Android ContentResolver.insert() throws NullPointerException 不能使用 MediaStore.Images.Media.insertImage - can not use MediaStore.Images.Media.insertImage MediaStore.Images.Media.insertImage()错误 - MediaStore.Images.Media.insertImage() error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM