从Android使用Google Mirror API附加图片不起作用

时间:2014-06-11 11:40:00

标签: java android google-glass google-mirror-api

我尝试使用java客户端libray将静态卡插入Google Glass时间线。它适用于带有附加图标的文本和菜单项,但在我尝试添加媒体项时失败。

当我插入元数据和附件时,只有元数据显示在时间轴卡中。

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

当我单独插入元数据和图像时,只显示元数据,我得到404未找到错误。

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

这是我的代码:

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"; 
        }       
}

基本上可以使用Android App中的Mirror Api附加图像吗? 非常感谢!

1 个答案:

答案 0 :(得分:0)

您的代码存在一些问题:

  1. timelineItem.setHtml(htmlMessage)屏蔽了背景图像,因此图片没有显示出来。我将代码更改为timelineItem.setTex(message)。图像显示在其上方的消息覆盖。

  2. 附件代码不正确(我认为contentType设置不正确)我将此代码更改为此代码。 Mirror API不再抱怨身份验证。猫图片很好地显示在时间轴上。

    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"; 
    }