Search code examples
javaandroidgoogle-app-enginerestblobstore

Upload Photo Android to Blobstore GAE


First, I'm french, so excuse my english.. I've traveled stackoverflow to find my answer, but without success.. I'm beginner in Android and Java Language.

I would like to upload photo from my android app to Blobstore. I read these article and success them:

But I've no idea how write code in my android app to upload photo to blobstore...

Here is my code:

Serve.java

public class Serve extends HttpServlet {
    private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res)
        throws IOException {
            BlobKey blobKey = new BlobKey(req.getParameter("blob-key"));
            blobstoreService.serve(blobKey, res);
        }
}

Upload.java

public class Upload extends HttpServlet {
    private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {

        Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
        BlobKey blobKey = blobs.get("myFile");

        if (blobKey == null) {
            res.sendRedirect("/");
        } else {
            res.sendRedirect("/serve?blob-key=" + blobKey.getKeyString());
        }
    }
}

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new LongOperation().execute("");
    }

     private class LongOperation extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... params) {

                // Create a new HttpClient and Post Header
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://1-dot-ptm-blobstore.appspot.com/upload");

                try {

                    // Execute HTTP Post Request
                    HttpResponse response = httpclient.execute(httppost);

                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                }   


                return null;

            }
        }
}

When I launch my app I've an error from my app engine logs:

Must be called from a blob upload callback request.

But I don't really what I must do now ? Can you help me ? =) Thanks in advance !


Solution

  • Here's one simple solution: Upload to Appengine Blobstore in Android

    Another one is to use Cloud Endpoints, i.e. create a REST API endpoint and use it to POST image data from your app: https://developers.google.com/appengine/docs/java/endpoints/

    Also, more advanced example is the Mobile Backend Starter, which gives you a few more cool features: https://developers.google.com/cloud/samples/mbs/

    And here's an article that goes with Mobile Backend Starter:

    Connecting mobile developers to the cloud with Google Cloud Endpoints http://googlecloudplatform.blogspot.co.uk/2013/11/connecting-mobile-developers-to-the-cloud-with-google-cloud-endpoints.html