[原] Unity调用android版新浪微博

本文提供unity调用微博android版 SDK 分享图片,现有sdk支持路径调用分享图片 雨凇MOMO已实现;

本文教会大家如何通过byte流分享图片(官方API

pic true binary 要上传的图片,仅支持JPEG、GIF、PNG格式,图片大小小于5M。

1、到官网下载 微博 android sdk

2、更改StatusesAPI.java 中 upload函数如下

  /**
     * 上传图片并发布一条新微博,此方法会处理urlencode
     * @param content 要发布的微博文本内容,内容不超过140个汉字
     * @param file 要上传的图片,仅支持JPEG、GIF、PNG格式,图片大小小于5M。
     * @param lat 纬度,有效范围:-90.0到+90.0,+表示北纬,默认为0.0。
     * @param lon 经度,有效范围:-180.0到+180.0,+表示东经,默认为0.0。
     * @param listener
     */
    public void upload( String content,byte[] img, String lat, String lon,
            RequestListener listener) {
         。。。。。。。。。。
        params.add("pic", "");
        。。。。。。。。。。
        request( SERVER_URL_PRIX + "/upload.json", params, img, HTTPMETHOD_POST, listener);
    }

将WeiboAPI.java 重载request为

    protected void request( final String url, final WeiboParameters params,final byte[] img,
            final String httpMethod,RequestListener listener) {
        。。。。。。。。。。。。。。
        AsyncWeiboRunner.request(url, params, httpMethod, img, listener);
    }

将AsyncWeiboRunner.java中request 重载为

public static void request(final String url, final WeiboParameters params,
            final String httpMethod,final byte[] img, final RequestListener listener) {
        new Thread() {
            @Override
            public void run() {
                try {
                    String resp = HttpManager.openUrl(url, httpMethod, params,img);
                   ...............................
                } catch (WeiboException e) {
                    listener.onError(e);
                }
            }
        }.start();

    }

以上的操作 主要是为了把byte流当参数传进来

3、重载openUrl为

public static String openUrl(String url, String method, WeiboParameters params, byte[] img) throws WeiboException {
      ........................
            if (img.length!=0) {
                .............
                imageContentToUpload(bos, img);
            }
          ...................................
    }

    private static void imageContentToUpload(OutputStream out, byte[] img) throws WeiboException {
        if(img.length==0){
            return;
        }
     ..........................
        try {
           ........................

                out.write(img, 0, img.length);
        .................................
        } catch (IOException e) {
            throw new WeiboException(e);
        }
    }

以上所有需要函数更改完毕

可以调用   

StatusesAPI  statusesAPI=new StatusesAPI(MainActivity.accessToken);
  statusesAPI.upload(str+content,imgBytes,"0","0", MainActivity.this);发送微博 其中content、imgBytes从unity出送过来

unity中实现:

截屏函数:

    IEnumerator TakePhoto()
    {
        yield return new  WaitForEndOfFrame();//一定先要调用这个
        Texture2D photoTex = new Texture2D (Screen.width, Screen.height, TextureFormat.RGB24, false);
        photoTex.ReadPixels(new Rect(0.0f, 0.0f, Screen.width * 1.0f, Screen.height*1.0f), 0, 0);
        photoTex.Apply();  
        text.texture=photoTex;//text定义的texture  预览截到的屏幕
        b = photoTex.EncodeToPNG();//将图片转成2进制流
    }

发送微博

    #if UNITY_ANDROID && !UNITY_EDITOR
                jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                Debug.Log("1111111111111111111");
                jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
               Debug.Log("2222222222221");

              jo.Call("sendSinaWeiBo","ASASASAS",b);//调用android中的sendSinaWeiBo发送微博
              #endif

android 工程下载地址

unity3d 下载地址

 


   

posted @ 2013-06-04 14:37  U_探索  阅读(1217)  评论(0编辑  收藏  举报