【问题标题】:Google App Engine Backend - Java Data Model - BitmapGoogle App Engine 后端 - Java 数据模型 - 位图
【发布时间】:2014-09-02 13:23:16
【问题描述】:

我正在编写一个应用程序,它允许用户拍照、添加一些标题和描述并将其上传到服务器。那个包裹(图片+标题+描述)被命名为“礼物”。

我使用了 Google 的自动后端引擎生成器,并添加了一个名为 Gift 的 @Entity 类,其中包含所有变量和一个构造函数,用于在一个名为 AppEngine 的新项目中构建一个“Gift”。 (我遵循了这个 Google 教程:https://developers.google.com/eclipse/docs/endpoints-addentities)。

我的问题基本上是,我如何与之前在我的应用程序内的主包上,现在在 AppEngine src 文件夹中的“礼物”类进行交互?

例如,以前,每当我想创建一个新的“礼物”并上传它时,我只是使用它的构造函数实例化一个新的“礼物”。现在,这是不可能的,因为 Eclipse 迫使我在自动生成的包(com.package.app.giftendpoit.model)中使用“Gift”类。

新的 Gift 类(称为 Java 数据模型)以以下几行开头:

/**
 * Model definition for Gift.
 * This is the Java data model class that specifies how to parse/serialize
 * into the JSON that is transmitted over HTTP when working with the giftendpoint. 
 * @author Google, Inc.
 */

@SuppressWarnings("javadoc")
public final class Gift extends com.google.api.client.json.GenericJson {

@com.google.api.client.util.Key
private Bitmap bmp;

(...)

当我尝试在我的应用程序上创建一个新礼物并设置它的位图时,我在 Eclipse 上收到以下错误消息:

Gift 类型中的 setBmp(com.package.app.giftendpoint.model.Bitmap) 方法不适用于参数 (android.graphics.Bitmap)

这是自动生成的 AppEngine 项目中仍然存在但不在我的应用程序中的原始 Gift 类的一部分:

@Entity
public class Gift {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String title;
private String description;
private Bitmap bmp;

public Gift(String title, String description, Bitmap bmp) {
    super();
    this.title = title;
    this.description = description;
    this.bmp = bmp;
}

//Getters & Setters (ie. SetBmp (Bitmap bitmap)...)

礼物的标题和描述设置没有错误,但位图没有设置。这是我用来执行此操作的代码:

public class EndpointsTask extends AsyncTask<Context, Integer, Long> {
    protected Long doInBackground(Context... contexts) {

           Giftendpoint.Builder endpointBuilder = new  Giftendpoint.Builder(
          AndroidHttp.newCompatibleTransport(),
          new JacksonFactory(),
          new HttpRequestInitializer() {
          public void initialize(HttpRequest httpRequest) { }
          });
           Giftendpoint endpoint = CloudEndpointUtils.updateBuilder(
  endpointBuilder).build();
  try {
      gift = new Gift();
      gift.setTitle(title);
      gift.setDescription(description);
      gift.setBmp(bmp); // Error:The method setBmp(com.package.app.giftendpoint.model.Bitmap) in the type Gift is not applicable for the arguments (android.graphics.Bitmap)

      Gift result = endpoint.insertGift(gift).execute();
  } catch (IOException e) {
    e.printStackTrace();
  }
      return (long) 0;
    }
}

关于如何解决此问题的任何提示?

谢谢

【问题讨论】:

  • 我只是将模型中所有引用位图的变量和方法更改为 android.graphics.Bitmap 而不仅仅是位图(它被称为 com.packege.app.giftendpoint.model。位图)。我会在这里做一些测试,看看这个变化是否有好处。
  • 上面说的不行,因为每次重建项目,android.graphics.Bitmap都会在Gift Model上改回Bitmap。

标签: java android eclipse google-app-engine bitmap


【解决方案1】:

好的,刚刚发现我应该使用 Blob 而不是位图。这是将图像转换为 blob 的代码:

    byte[] byteArray = getIntent().getByteArrayExtra("image");
    bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    bmpBlob = Base64.encodeToString(byteArray, Base64.DEFAULT);

我还更新了我的实体以使用 Blob 而不是位图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多