Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: base64 string no longer work with the show function on Android #68

Closed
tiennp-dev opened this issue Sep 3, 2024 · 3 comments · Fixed by #69
Closed

bug: base64 string no longer work with the show function on Android #68

tiennp-dev opened this issue Sep 3, 2024 · 3 comments · Fixed by #69
Assignees
Labels
bug/fix Something isn't working needs: triage platform: android Android platform

Comments

@tiennp-dev
Copy link

Plugin version: On the latest version 4.0.0

Platform(s): Android

Current behavior:
I am using the show function of the Photoviewer plugin in my Ionic app with a Base64 string (e.g., "data:image/jpeg;base64,/9j/4AAQSkZJ.....TY3OD=="). However, this results in an error:

image
image

I've been able to resolve this issue in 'one' mode but, due to my limited experience with Java, I haven't been able to create a PR to address the issue fully. Please refer to the Related Code section for more details.

Expected behavior: The plugin should support Base64 strings as described in the README.

Steps to reproduce: N/A

Related code:
Here is my usage in my Ionic app, which very simple

      if (Capacitor.isNativePlatform()) {
        await PhotoViewer.show({
          images: [
            { url: base64string, title: (new Date()).getTime().toString() }
          ],
          options: {
            compressionquality: 0.8,
            share: true,
          },
          mode: 'one',
          startFrom: 0
        })
        return;
      }

The issue seems to be related to this file: ImageFragment.kt. Below is a snippet of the code where the issue occurs:

        val mImageToBeLoaded = ImageToBeLoaded()
        val toBeLoaded = image.url?.let { mImageToBeLoaded.getToBeLoaded(it) }

        if (toBeLoaded is String) {
            // load image from url
            val lazyHeaders = LazyHeaders.Builder()
            for (key in customHeaders.keys()) {
                customHeaders.getString(key)?.let { lazyHeaders.addHeader(key, it) }
            }
            val glideUrl = GlideUrl(toBeLoaded, lazyHeaders.build())
            GlideApp.with(appContext)
                .asBitmap()
                .load(glideUrl)
                .fitCenter()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(ivTouchImage)
        }

In cases when toBeLoaded returns a Base64 string, the current implementation doesn't handle it properly.
I added the following check to address this, and it worked as expected.
I believe this issue is related to this commit, which introduced support for custom headers but didn't account for Base64 strings.

        if (toBeLoaded is String) {
            if (toBeLoaded.contains("base64")) {
                GlideApp.with(appContext)
                    .asBitmap()
                    .load(toBeLoaded)
                    .fitCenter()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(ivTouchImage)
            }
            else {
                // load image from url
                val lazyHeaders = LazyHeaders.Builder()
                for (key in customHeaders.keys()) {
                    customHeaders.getString(key)?.let { lazyHeaders.addHeader(key, it) }
                }
                val glideUrl = GlideUrl(toBeLoaded, lazyHeaders.build())
                GlideApp.with(appContext)
                    .asBitmap()
                    .load(glideUrl)
                    .fitCenter()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(ivTouchImage)
            }
        }

Other information: This is my first time creating an issue, so please feel free to correct me if I’ve made any mistakes.

@tiennp-dev tiennp-dev added bug/fix Something isn't working needs: triage labels Sep 3, 2024
@robingenz
Copy link
Member

@shiv19 Could you take a look at this?

@robingenz robingenz added the platform: android Android platform label Sep 3, 2024
@shiv19
Copy link
Contributor

shiv19 commented Sep 3, 2024

@robingenz, I will submit a fix for this today.
@tiennp-dev, thanks for reporting this issue!

@tiennpdev
Copy link

tiennpdev commented Sep 4, 2024

@shiv19 @robingenz Thank you both for your prompt improvements!!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug/fix Something isn't working needs: triage platform: android Android platform
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants