ahmetvefa53
ahmetvefa53

Reputation: 611

Android show medias with old date on galery

I want to insert media in mediastore with a old date. Example ;

   val values = ContentValues()
    val extension = fileName.fileExtension()
    val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
    values.apply {
        put(MediaStore.MediaColumns.DISPLAY_NAME, fileName)
        put(MediaStore.MediaColumns.MIME_TYPE, mimeType)
        put(MediaStore.MediaColumns.DATE_ADDED, 1542628)
        put(MediaStore.MediaColumns.DATE_MODIFIED, 1542628)
        put(MediaStore.MediaColumns.DATE_TAKEN, 1542628983)
        put(MediaStore.MediaColumns.MIME_TYPE, mimeType)
        if (AndroidUtils.isAtLeastQ()) {
            put(MediaStore.MediaColumns.RELATIVE_PATH, getRelativePath(fileType))
        }
    }
    val url = resolver.insert(getMediaCollection(fileType, ExternalPrimary), values)

1542628983 = 19/11/2018 but after insert , media is shown on Today

How can I set a galery date on a media?

Upvotes: 1

Views: 156

Answers (1)

Dev4Life
Dev4Life

Reputation: 3317

Try this to save your bitmap into mediastore. You can get the bitmap with different methods. So after that, try this. Replace the values you want to modify with the new values.

fun saveBitmap(
    context: Context, bitmap: Bitmap?,
    displayName: String,
    directory: String
) {
    val values = ContentValues()
    values.put(MediaStore.MediaColumns.DISPLAY_NAME, displayName)
    values.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
    values.put(
        MediaStore.MediaColumns.RELATIVE_PATH,
        Environment.DIRECTORY_DCIM + File.separator + File(directory).name
    )
    val uri = context.contentResolver
        .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
    try {
        val stream =
            context.contentResolver.openOutputStream(uri!!)
        bitmap?.compress(Bitmap.CompressFormat.JPEG, 100, stream)
    } catch (e: Exception) {
        Log.e("TAG", "saveBitmapExc: ${e.message}")
        e.printStackTrace()
    }
}

Upvotes: 0

Related Questions