user3339562
user3339562

Reputation: 1385

Displaying Google Datastore Blob image inside Android application with Endpoints

I am not sure am I doing this correctly so I would like to ask community for opinion. I have my model made like this:

@Entity
public class Info {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Key key;
      private Blob logo;

      public Info(Blob logo) {
        super();
        this.logo = logo;
    }

    public byte[] getLogo() {
        if (logo == null) {
            return null;
        }

        return logo.getBytes();
    }

    public void setLogo(byte[] bytes) {
        this.logo = new Blob(bytes);
    }  
}

I set my Blob image like this:

info.setProperty("logo", new Blob(imageInByte));

After I generate client endpoint libraries, my blob image is defined as String and that is weird to me. I have following methods for my Info object on my client:

@com.google.api.client.util.Key
private java.lang.String logo;

/**
   * @see #decodeLogo()
   * @return value or {@code null} for none
   */
  public java.lang.String getLogo() {
    return logo;
  }

  /**

   * @see #getLogo()
   * @return Base64 decoded value or {@code null} for none
   *
   * @since 1.14
   */
  public byte[] decodeLogo() {
    return com.google.api.client.util.Base64.decodeBase64(logo);
  }

  /**
   * @see #encodeLogo()
  public Info setLogo(java.lang.String logo) {
    this.logo = logo;
    return this;
  }

  /**

   * @see #setLogo()
   *
   * <p>
   * The value is encoded Base64 or {@code null} for none.
   * </p>
   *
   * @since 1.14
   */
  public Info encodeLogo(byte[] logo) {
    this.logo = com.google.api.client.util.Base64.encodeBase64URLSafeString(logo);
    return this;
  }

Now I am not sure what do I need to do to display that image inside my ImageView. I tried this:

Bitmap bm = BitmapFactory.decodeByteArray(info.decodeLogo(), 0, info.decodeLogo().length);

But I receive the following error in LogCat:

I/System.out(26730): resolveUri failed on bad bitmap uri: android.graphics.Bitmap@40fba0e0

What am I doing wrong here? Thank you for suggestions :)

Upvotes: 0

Views: 421

Answers (1)

Mahdi Moalla
Mahdi Moalla

Reputation: 11

The String is the base64 encode of the byte array. But it seems that the base64 encoder com.google.api.client.util.Base64.encodeBase64URLSafeString() gives the wrong result.

I used another base64 encoder such as the one presented in https://stackoverflow.com/a/16899776/3842997 and the operation succeeded.

//byte[] Data ;

StringBuffer out = new StringBuffer();
out.append(Base64Coder.encode(Data, 0, Data.length));
note.setData(out.toString());

Upvotes: 1

Related Questions