copy dates from mediastore from source to destination after copying files
This commit is contained in:
parent
0c50b68a30
commit
03d8f48c73
3 changed files with 33 additions and 6 deletions
|
@ -31,7 +31,7 @@ ext {
|
|||
propMinSdkVersion = 16
|
||||
propTargetSdkVersion = propCompileSdkVersion
|
||||
propVersionCode = 1
|
||||
propVersionName = '2.31.1'
|
||||
propVersionName = '2.31.4'
|
||||
kotlin_version = '1.1.51'
|
||||
support_libs = '25.3.1'
|
||||
}
|
||||
|
|
|
@ -101,9 +101,12 @@ class CopyMoveTask(val activity: BaseSimpleActivity, val copyOnly: Boolean = fal
|
|||
|
||||
inputStream = FileInputStream(source)
|
||||
inputStream.copyTo(out!!)
|
||||
activity.scanFile(destination) {}
|
||||
if (source.length() == destination.length())
|
||||
activity.scanFile(destination) {
|
||||
activity.copyDates(source, destination)
|
||||
}
|
||||
if (source.length() == destination.length()) {
|
||||
mMovedFiles.add(source)
|
||||
}
|
||||
} finally {
|
||||
inputStream?.close()
|
||||
out?.close()
|
||||
|
|
|
@ -189,16 +189,40 @@ fun Context.updateInMediaStore(oldFile: File, newFile: File): Boolean {
|
|||
put(MediaStore.MediaColumns.TITLE, newFile.name)
|
||||
}
|
||||
val uri = getFileUri(oldFile)
|
||||
val where = "${MediaStore.MediaColumns.DATA} = ?"
|
||||
val args = arrayOf(oldFile.absolutePath)
|
||||
val selection = "${MediaStore.MediaColumns.DATA} = ?"
|
||||
val selectionArgs = arrayOf(oldFile.absolutePath)
|
||||
|
||||
return try {
|
||||
contentResolver.update(uri, values, where, args) == 1
|
||||
contentResolver.update(uri, values, selection, selectionArgs) == 1
|
||||
} catch (e: SQLiteConstraintException) {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fun Context.copyDates(oldFile: File, newFile: File) {
|
||||
val projection = arrayOf(
|
||||
MediaStore.Images.Media.DATE_TAKEN,
|
||||
MediaStore.Images.Media.DATE_MODIFIED)
|
||||
val uri = MediaStore.Files.getContentUri("external")
|
||||
val selection = "${MediaStore.MediaColumns.DATA} = ?"
|
||||
var selectionArgs = arrayOf(oldFile.absolutePath)
|
||||
val cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
|
||||
cursor?.use {
|
||||
if (cursor.moveToFirst()) {
|
||||
val dateTaken = cursor.getLongValue(MediaStore.Images.Media.DATE_TAKEN)
|
||||
val dateModified = cursor.getIntValue(MediaStore.Images.Media.DATE_MODIFIED)
|
||||
|
||||
val values = ContentValues().apply {
|
||||
put(MediaStore.Images.Media.DATE_TAKEN, dateTaken)
|
||||
put(MediaStore.Images.Media.DATE_MODIFIED, dateModified)
|
||||
}
|
||||
|
||||
selectionArgs = arrayOf(newFile.absolutePath)
|
||||
contentResolver.update(uri, values, selection, selectionArgs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// avoid these being set as SD card paths
|
||||
private val physicalPaths = arrayListOf(
|
||||
"/storage/sdcard1", // Motorola Xoom
|
||||
|
|
Loading…
Reference in a new issue