Search code examples
javaandroidgoogle-glassgoogle-mirror-api

Attaching Images with Google Mirror API from Android doesn't work


i'm trying to insert a static card to the Google Glass Timeline using the java client libray. It works fine for text and menu items with attached icons, but it fails when i'm trying to add a media item.

When i insert metadata and an attachment, only the metadata is displayed in the timeline card.

service.timeline().insert(timelineItem, mediaContent).setOauthToken((String) userToken).execute();  

When i separately insert metadata and the image, only the metadata is shown and i get an 404 Not Found Error.

service.timeline().insert(timelineItem).setOauthToken((String) userToken).execute();     
service.timeline().attachments().insert(itemId, mediaContent).setOauthToken((String) userToken).execute();

This is my code:

import com.google.api.client.extensions.android.json.AndroidJsonFactory;
import com.google.api.client.http.InputStreamContent;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.services.mirror.Mirror;
import com.google.api.services.mirror.model.MenuItem;
import com.google.api.services.mirror.model.MenuValue;
import com.google.api.services.mirror.model.TimelineItem;

protected String doInBackground(Object... params) {

        String message = "hello world!";
        String appName = "MyApp";          
        String contentType = "";
        String itemId = "21341234";
        InputStream attachmentIS = null;
        String htmlMessage = "<article>\n <section>\n <p class=\"text-auto-size\"> "
                + message + " </p>\n <p>\n </section>\n<footer>"
                + appName + "</footer></p> </article>\n";

        Mirror service = new Mirror.Builder(new NetHttpTransport(), new AndroidJsonFactory(), null)
        .setApplicationName(appName).build();

        try {           
            URL url = new URL("http://www.iconhot.com/icon/png/halloween-2009/256/cat-2.png");
            attachmentIS = url.openStream();
            //get content type
            HttpURLConnection connection = (HttpURLConnection)  url.openConnection();
            connection.setRequestMethod("HEAD");
            connection.connect();
            contentType = connection.getContentType();
            Log.d("contentType", contentType);
        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       

        InputStreamContent mediaContent = new InputStreamContent(contentType, attachmentIS);

        //Add Menu Items
        List<MenuItem> menuItemList = new ArrayList<MenuItem>();
        menuItemList.add(new MenuItem().setAction("DELETE"));

        List<MenuValue> menuValues = new ArrayList<MenuValue>();
        menuValues.add(new MenuValue().setIconUrl("http://www.iconhot.com/icon/png/halloween-2009/256/cat-2.png")
            .setDisplayName("Drill In"));
        menuItemList.add(new MenuItem().setValues(menuValues).setId("drill").setAction("CUSTOM"));

        //Create Timeline Item
        TimelineItem timelineItem = new TimelineItem();
        timelineItem.setHtml(htmlMessage);
        timelineItem.setMenuItems(menuItemList);
        timelineItem.setId(itemId);


        try{
            if (contentType != null && contentType.length() > 0) {
                service.timeline().insert(timelineItem, mediaContent).setOauthToken((String) params[0]).execute();
                //service.timeline().insert(timelineItem).setOauthToken((String) params[0]).execute();  
                //service.timeline().attachments().insert(itemId, mediaContent).setOauthToken((String) params[0]).execute();
                return "success"; 
            }
            return "failure"; 
        }
        catch (Exception e) {               
            Log.d("Failure", e.getMessage());
            return "failure"; 
        }       
}

Is it basically possible to attach images using the Mirror Api from an Android App? Thanks a lot!


Solution

  • There are a couple of problem with your codes:

    1. timelineItem.setHtml(htmlMessage) masked off the background image so the picture did not show up. I changed the code to timelineItem.setTex(message). The image showed up with the message overlay on top of it.

    2. The code for attachment is not correct (I think contentType was not set correctly) I changed to this the code below. Mirror API does not complain about authentication anymore. The cat picture shows up nicely on the timeline.

      String contentType = "image/jpeg";
      InputStream attachmentIS = null;
      try {
          URL url = new URL("http://www.iconhot.com/icon/png/halloween-2009/256/cat-2.png");
          attachmentIS = url.openStream();
      } catch (MalformedURLException e1) {
          attachmentIS = null;
          e1.printStackTrace();
      } catch (IOException e) {
          attachmentIS = null;
          e.printStackTrace();
      }
      if (attachmentIS == null)
         return "failure";
      InputStreamContent mediaContent = new InputStreamContent(contentType, attachmentIS);
      try {
          service.timeline().insert(timelineItem, mediaContent).setOauthToken((String) params[0]).execute();
          return "success"; 
      }
      catch (Exception e) {               
          Log.d("Failure", e.getMessage());
          return "failure"; 
      }