user248187
user248187

Reputation: 131

Upload Image from Android to GAE datastore

How to upload image from Android Client to GAE (Google App Engine) datastore ?

no tutorials found yet, have tried for last 7 days.

Upvotes: 2

Views: 1046

Answers (2)

user248187
user248187

Reputation: 131

OK, i am able to upload image like this

  1. Create POJO Photo

public class photo {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;



public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

private Blob blobImage;

public Blob getBlobImage() {
    return blobImage;
}

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

private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

private String type;

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

}

  1. Generate Cloud Endpoint (right clicking -> google -> generate cloud endpoint)
  2. Genereate CLient Side Libraries (right click project -> google -> generate client libraries)
  3. In your android application -> open photoendpoint.model (package) -> Photo.java replace your public byte[] decodeBlobImage() { } to this

    public byte[] decodeBlobImage() { return com.google.api.client.util.Base64.decodeBase64(blobImage); }

  4. perform this in your activity

                    Photo photo = new Photo();
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
        R.drawable.sherlock);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        photo.encodeBlobImage(byteArray);
        photo.setName("Sherlock");
        photo.setName("sherlock");
        photo.setType("image/jpeg");
        photo.setId((long) 8232);
        Photoendpoint.Builder builder = new Photoendpoint.Builder(
        AndroidHttp.newCompatibleTransport(), new JacksonFactory(),
                null);
        builder = CloudEndpointUtils.updateBuilder(builder);
    
        Photoendpoint endpoint = builder.build();
    
        Log.d("Exception", "inserting image");
        try {
            Log.d("Exception", "inserting");
            endpoint.insertphoto(photo).execute();
    
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.d("Exception", "" + e);
        }
    

Upvotes: 3

Romin
Romin

Reputation: 8816

First, you need to decide how large your image file that you are uploading is going to be. This is because if you are using a Datastore that at best you could use a TEXT field that could save only upto 1 MB of data.

I suggest that you look at using the Blobstore or better still the Google Cloud Storage service as a way to save your Image Data.

The steps will be roughly as follows:

  • Do a POST from your Android code with the Image bytes.
  • On the App Engine side, receive the bytes, encode them and then use the Google Cloud Storage Service to save the data. You could then use the GCS Identifier and save that in the Datastore as a reference to the image.

Take a look at the following thread for sample code.

Upvotes: 2

Related Questions