Reputation: 131
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
Reputation: 131
OK, i am able to upload image like this
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;
}
}
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); }
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
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:
Take a look at the following thread for sample code.
Upvotes: 2