kim icon indicating copy to clipboard operation
kim copied to clipboard

Added method to help usage on Android

Open ravenfeld opened this issue 8 months ago • 4 comments

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.

ravenfeld avatar Apr 03 '25 06:04 ravenfeld

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

StefanOltmann avatar Apr 04 '25 03:04 StefanOltmann

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

ravenfeld avatar Apr 04 '25 05:04 ravenfeld

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.

StefanOltmann avatar Apr 04 '25 07:04 StefanOltmann

I will send you the "if" of the Android version as soon as possible.

ravenfeld avatar Apr 04 '25 07:04 ravenfeld

       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            inputStream.readNBytes(count)
        } else {
           ??
        }

I'm not good enough to find a replacement function.

ravenfeld avatar May 09 '25 07:05 ravenfeld

       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?

StefanOltmann avatar May 09 '25 07:05 StefanOltmann

I will test and let you know

ravenfeld avatar May 09 '25 08:05 ravenfeld

Here is my modification, it seems to work on my test phones

ravenfeld avatar May 09 '25 08:05 ravenfeld

Thanks, I will have to look at the compilation error.

StefanOltmann avatar May 09 '25 21:05 StefanOltmann

That was the issue: image

StefanOltmann avatar May 16 '25 09:05 StefanOltmann

I fixed it in the update branch.

StefanOltmann avatar May 16 '25 10:05 StefanOltmann

It's available now. Thank you for your contribution. :)

StefanOltmann avatar May 17 '25 02:05 StefanOltmann

I saw it yesterday, but I haven't had time to upgrade yet. Thank you for this very useful library for my application.

ravenfeld avatar May 17 '25 06:05 ravenfeld