如何将图像从Android上传到谷歌云(Java)?

如何将图像从Android上传到谷歌云(Java)?,java,android,google-app-engine,jsp,Java,Android,Google App Engine,Jsp,我正在开发一个基于Android的应用程序,与谷歌云交互。我需要上传图片到云端。我已经为PHP+Android开发了代码,但我不确定如何在googlecloud(JAVA)中处理它 Android代码: public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { if (requestCode == SELECT_PI

我正在开发一个基于Android的应用程序,与谷歌云交互。我需要上传图片到云端。我已经为PHP+Android开发了代码,但我不确定如何在googlecloud(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();
        }
    }
}
activityresult上的公共void(int-requestCode、int-resultCode、Intent-data){
if(resultCode==RESULT\u OK){
if(requestCode==选择图片){
Uri selectedImageUri=data.getData();
字符串selectedImagePath=getPath(selectedImageUri);
输入流为;
BitmapFactory.decodeFile(data.getData().toString());
位图bitmapOrg=BitmapFactory.decodeFile(selectedImagePath);//BitmapFactory.decodeResource(getResources(),R.drawable.gallery);
ByteArrayOutputStream bao=新建ByteArrayOutputStream();
压缩(Bitmap.CompressFormat.JPEG,90,bao);
字节[]ba=bao.toByteArray();
字符串ba1=Base64.encodeToString(ba,1);
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“图像”,ba1));
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.USER_代理,“自定义用户代理”);
HttpPost HttpPost=新
HttpPost(“http://umair-p.appspot.com/imageupload");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
Toast.makeText(Main.this,is.toString(),Toast.LENGTH_LONG).show();
//Toast.makeText(Main.this,“图片共享”,Toast.LENGTH_LONG.show();
}
捕获(例外e){
Toast.makeText(Main.this,“Exception:+e.toString(),Toast.LENGTH\u LONG.show();
}
//Toast.makeText(Main.this,data.getData().toString(),Toast.LENGTH_SHORT).show();
}
}
}
PHP代码(工作正常):



上面的解决方案很好,但我需要上传图像到谷歌云。有什么帮助吗?

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

首先,执行HTTP GET访问

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


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

什么是“谷歌云”?您的意思是要将图像发布到appspot上托管的web应用程序吗?如果你已经有了一个可以实现这一点的Android应用程序,那么你很快就会有一个可以实现这一点的非Android Java应用程序了?你的具体目标是什么?你是说你想让appspot上托管的Java web应用程序与Google应用程序引擎对话吗?无论如何,我不明白你的问题。我为你的含糊不清道歉。我希望您能指导我编写JSP代码,相当于我在文章中所写的PHP代码。并评论我的做法是否正确。因为我是JSP新手,我不知道这个解决方案是否有效。你好,comet,你能帮我把android代码上传到谷歌云存储吗?如果您曾经处理过示例代码,请将其发送给我,您将非常感激…我的邮件id:chavinash792@gmail.comThis只有当他想使用blobstore时才需要,如果图像小于1MB,他可能不想。无论如何,链接到blobstore文档()会很有帮助。
<?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>';
?>