使用端点在Android应用程序中显示Google Datastore Blob图像

时间:2022-10-30 23:12:18

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:

我将Blob图像设置为:

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:

在生成客户端端点库之后,我的blob图像被定义为String,这对我来说很奇怪。我的客户端上的Info对象有以下方法:

@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:

现在我不确定在ImageView中显示该图像需要做什么。我试过这个:

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

But I receive the following error in LogCat:

但是我在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 :)

我在这做错了什么?谢谢你的建议:)

1 个解决方案

#1


1  

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.

String是字节数组的base64编码。但似乎base64编码器com.google.api.client.util.Base64.encodeBase64URLSafeString()给出了错误的结果。

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

我使用了另一个base64编码器,如https://*.com/a/16899776/3842997中提供的编码器,操作成功。

//byte[] Data ;

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

#1


1  

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.

String是字节数组的base64编码。但似乎base64编码器com.google.api.client.util.Base64.encodeBase64URLSafeString()给出了错误的结果。

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

我使用了另一个base64编码器,如https://*.com/a/16899776/3842997中提供的编码器,操作成功。

//byte[] Data ;

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