Reputation: 7442
I am developing an Android based application that interacts with the Google Cloud. I need to upload Pictures to the Cloud. I have already developed the code for PHP+Android but I am not sure how to handle it in Google Cloud (JAVA).
Android Code:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
InputStream is;
BitmapFactory.decodeFile(data.getData().toString());
Bitmap bitmapOrg = BitmapFactory.decodeFile(selectedImagePath); //BitmapFactory.decodeResource(getResources(),R.drawable.gallery);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
String ba1 = Base64.encodeToString(ba, 1);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",ba1));
try{
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Custom user agent");
HttpPost httppost = new
HttpPost("http://umair-p.appspot.com/imageupload");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Toast.makeText(Main.this, is.toString(), Toast.LENGTH_LONG).show();
//Toast.makeText(Main.this, "Picture Shared", Toast.LENGTH_LONG).show();
}
catch(Exception e){
Toast.makeText(Main.this, "Exception: " + e.toString(), Toast.LENGTH_LONG).show();
}
//Toast.makeText(Main.this, data.getData().toString(), Toast.LENGTH_SHORT).show();
}
}
}
PHP Code (that works fine):
<?php
$base=$_REQUEST['image'];
echo $base;
// base64 encoded utf-8 string
$binary=base64_decode($base);
// binary, utf-8 bytes
header('Content-Type: bitmap; charset=utf-8');
// print($binary);
//$theFile = base64_decode($image_data);
$file = fopen('test.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo '<img src=test.jpg>';
?>
The above solution is working fine but I need to upload image to Google Cloud. Any help?
Upvotes: 2
Views: 2174
Reputation: 4061
You need to generate an upload URL from you AppEngine application.
First, perform a HTTP GET to http://umair-p.appspot.com/imageupload
Then, on the server, call blobstoreService.createUploadUrl("/imageupload"). Return the URL that this generates in the HTTP response.
Finally, the Android app reads the URL from the response and uses it in the HTTP POST request to upload the image.
Upvotes: 3