Mino
Mino

Reputation: 264

What is the best way to store image from Android app to Datastore/Google Cloud Storage?

I'm trying to store a Google Maps snapshot (from Android) into the Datastore but i'm experiencing some troubles:

The snapshot is a bitmap converted to a byte array that i stringify but it crashed because of low surrogate character.

I solve this by changing StringUtils.newStringUtf8(byteAray) to new String(byteArray, Charset.forName("ISO-8859-15")).

Now it won't save my entity because "error 413: the request is too long".

For information, here's how i get the byte array:

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 50, stream);
    byte [] bytes = stream.toByteArray();

So how can i store that bitmap efficiently to the Datastore (or an other solution) to be able to get it back elsewhere in my Android app ?

Upvotes: 1

Views: 413

Answers (2)

Micro
Micro

Reputation: 10891

The error you are getting:

error 413: the request is too long

You could solve by making the snapshot smaller in the client. I'm pretty sure most snapshots of modern phones are going to be over 1MB, so you would definitely get this error since datastore can only take Base64 encoded string of under 1MB.

I recommend you shrink your image before encoding it and sending it to datastore.

If you want to maintain the original size, your other option is to use Google Cloud Storage, specifically, for your case, Signed upload URLS where you can create a signed URL and then upload your image to it

Upvotes: 1

Frank Wilson
Frank Wilson

Reputation: 3250

Note that for large binary objects you should use the Blobstore API ideally with Google Cloud Storage.

If you are sure that your image files will always be under 1MB you can use the Blob entity property type.

Upvotes: 1

Related Questions