如何将Android上的图像上传到Google Cloud(Java)?

如何将Android上的图像上传到Google Cloud(Java)?

问题描述:

我正在开发一个基于Android的应用程序,它可以与Google Cloud进行交互。我需要将图片上传到云端。我已经开发了PHP + Android的代码,但我不确定如何在Google Cloud(JAVA)中处理它。如何将Android上的图像上传到Google Cloud(Java)?

的Android代码:

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代码(即正常工作):

<?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>'; 
?> 

上述解决方案工作正常,但我需要上传图像谷歌云。任何帮助?

+0

“Google Cloud”是什么意思?你的意思是你想将图像发布到appspot上托管的Web应用程序?如果你已经有了一个可以工作的Android应用程序,那么你有一个非Android Java应用程序可以做到这一点?你具体在哪里卡住? – 2011-05-05 11:40:00

+0

您的意思是说,您正试图让托管在appspot上的Java Web应用程序与Google App Engine对话?无论如何,我不明白你的问题。 – 2011-05-05 11:46:07

+0

我对此含糊不清。我希望你能够引导我写JSP相当于我放在我的文章中的PHP代码。如果我这样做的方式正确与否,也可以发表评论。由于我是JSP新手,我不知道这个解决方案是否可行。 – 2011-05-05 11:48:48

您需要从AppEngine应用程序生成上传URL。

首先,执行HTTP GET来http://umair-p.appspot.com/imageupload

然后,在服务器上,呼叫blobstoreService.createUploadUrl( “/ imageupload”)。返回在HTTP响应中生成的URL。

最后,Android应用程序从响应中读取URL并在HTTP POST请求中使用它来上传图像。

+2

这只有当他想要使用blobstore时 - 如果图片小于1MB ,他可能不想。无论如何,链接到blobstore文档(http://code.google.com/appengine/docs/python/blobstore/)将会很有帮助。 – 2011-05-05 16:33:20