I am trying to upload an image from Android directly to Google cloud storage. But the API does not seem to work. They have some Java samples which are tied to the App engine. I don't see any sample which is proven to work on Android.
On Android, I tried using the json api to upload an image. I am able to upload an image object but it seems to be corrupt. Moreover generating the authentication token seems to be tricky too.
I am struck with this right now. Did anyone on this earth ever tried uploading an image/video from Android using Java client or Json API and succeeded? Can someone point me in the right direction please. It has been very disappointing experience with this Storage api from Google. Please share your experiences if someone did it. Below is the code that I am trying from Android while trying to use the GCS's JSON API.
private static String uploadFile(RichMedia media) { DefaultHttpClient client = new DefaultHttpClient(); Bitmap bitmap = BitmapUtils.getBitmap(media.getLocalUrl()); HttpPost post = new HttpPost(GCS_ROOT + media.getLocalUrl() + "_" + System.currentTimeMillis()); if(media.getType() == RichMedia.RichMediaType.PICTURE) { post.setHeader("Content-Type", "image/jpeg"); } else { post.setHeader("Content-Type", "video/mp4"); } post.setHeader("Authorization", "AIzaSyCzdmCMwiMzl6LD7R2obF0xSgnnx5rEfeI"); //post.setHeader("Content-Length", String.valueOf(bitmap.getByteCount())); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); byte[] byteArray = stream.toByteArray(); try { post.setEntity(new StringEntity(new Gson().toJson(byteArray).toString())); HttpResponse response = client.execute(post); BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String eachLine = null; StringBuilder builder = new StringBuilder(); while ((eachLine = reader.readLine()) != null) { builder.append(eachLine); } L.d("response = " + builder.toString()); JSONObject object = new JSONObject(builder.toString()); String name = object.getString("name"); return name; } catch (IOException e) { L.print(e); } catch (JSONException e) { L.print(e); } return null; }
I am running into two issues here.
The file which got uploaded to the server is corrupt. It is not the same image that I uploaded. It is currupt.
The authorization key expires very often. In my case, I am using the auth code generated by gsutil.
To upload a file to Cloud Storage, you first create a reference to the full path of the file, including the file name. Once you've created an appropriate reference, you then call the putBytes() , putFile() , or putStream() method to upload the file to Cloud Storage.
Drag and drop the desired files from your desktop or file manager to the main pane in the Google Cloud console. Click the Upload Files button, select the files you want to upload in the dialog that appears, and click Open.
Cloud Storage provides the Signed URL feature to let individual end users perform specific actions. Signed URL makes it possible to generate temporary credentials valid only for a specific end user to securely upload a file. The Google Cloud official client library makes it easy to generate a Signed URL.
Since no one is answering this question, let me update the way I solved this problem. I ended up following this https://github.com/pliablematter/simple-cloud-storage project.
I could upload Pictures/Videos to GCS from my Android app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With