Added method to help usage on Android
I've added my own methods to use Kim on Android. I'm having issues adding tags directly, which I've worked around but I'm not proud of.
fun addTag(
byteReader: () -> ByteReader?,
byteWriter: () -> ByteWriter?,
block: (outputSet: TiffOutputSet) -> Unit
) {
val metadata = byteReader()?.let {
Kim.readMetadata(it)
} ?: return
val outputSet: TiffOutputSet = metadata.exif?.createOutputSet() ?: TiffOutputSet()
block(outputSet)
byteReader()?.let { byteReader ->
// Currently bug with Android >29 with the direct byteWriter (Kim 0.22.1)
val byteArrayByteWriter = ByteArrayByteWriter()
JpegRewriter.updateExifMetadataLossless(
byteReader = byteReader,
byteWriter = byteArrayByteWriter,
outputSet = outputSet
)
byteWriter()?.use { byteWriter ->
byteWriter.write(byteArrayByteWriter.toByteArray())
}
}
}
It's starting to get old and I'm sorry about that, so I don't really remember why your methods didn't work properly.
Hi,
thanks for your PR. :)
The code you copied from Android, did you take that from https://android.googlesource.com/platform/prebuilts/fullsdk/sources/+/refs/heads/androidx-swiperefreshlayout-release/android-35/java/io/InputStream.java or what was the exact source?
If it’s GPL (like the linked file) we can’t take it, because the project needs to stay Apache 2 to enable commercial use.
New code needs to be written from scratch that does the same.
Kind regards,
Stefan
Yes, this is the code that comes from Android.
It was to avoid a version if, so if I understand correctly it's better to have a version if
I see.
In that case, we should use a version switch with the original code or implement the method ourselves.
The copied code is licensed under GPL, which, as you know, would affect the entire repository and make it unsuitable for commercial use.
I will send you the "if" of the Android version as soon as possible.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
inputStream.readNBytes(count)
} else {
??
}
I'm not good enough to find a replacement function.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { inputStream.readNBytes(count) } else { ?? }I'm not good enough to find a replacement function.
I guess there is none.
That's why I did this:
/*
* InputStream.readNBytes(count) is not available
* on older Android versions. So we need to read
* into a buffer.
*/
val buffer = ByteArray(count)
val bytes = inputStream.read(buffer)
return if (bytes == count)
buffer
else
buffer.slice(startIndex = 0, count = count)
I believe your solution to check the API level first and only fall back to this inefficient code for older devices is fine.
What do you think?
I will test and let you know
Here is my modification, it seems to work on my test phones
Thanks, I will have to look at the compilation error.
That was the issue:
I fixed it in the update branch.
It's available now. Thank you for your contribution. :)
I saw it yesterday, but I haven't had time to upgrade yet. Thank you for this very useful library for my application.