android平台 新浪微博开发 分享gif不能动的问题原因

在新浪文档中明明指明

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

即接口https://upload.api.weibo.com/2/statuses/upload.json

支持gif图片分享的。

但是把一个gif的路径传给新浪的sdk中的接口,分享上去的还是普通图片。


sdk代码跟踪:

Utility.java中有一个方法叫

public static String openUrl(Context context, String url, String method,
            WeiboParameters params, String file, Token token) throws WeiboException {...}

其中把接收到的文件路径这么处理的
Bitmap bf = BitmapFactory.decodeFile(file);
Utility.imageContentToUpload(bos, bf);

这样decode后生成的bitmap就是一页的,所有图片不会动。分享上去的跟普通图片一样。



修改方法:

把原来的方法:

private static void imageContentToUpload(OutputStream out, Bitmap imgpath)
            throws WeiboException {
        StringBuilder temp = new StringBuilder();

        temp.append(MP_BOUNDARY).append("\r\n");
        temp.append("Content-Disposition: form-data; name=\"pic\"; filename=\"")
                .append("news_image").append("\"\r\n");
        String filetype = "image/png";
        temp.append("Content-Type: ").append(filetype).append("\r\n\r\n");
        byte[] res = temp.toString().getBytes();
        BufferedInputStream bis = null;
        try {
            out.write(res);
            imgpath.compress(CompressFormat.PNG, 75, out);
            out.write("\r\n".getBytes());
            out.write(("\r\n" + END_MP_BOUNDARY).getBytes());
        } catch (IOException e) {
            throw new WeiboException(e);
        } finally {
            if (null != bis) {
                try {
                    bis.close();
                } catch (IOException e) {
                    throw new WeiboException(e);
                }
            }
        }
    }


修改如下:

private static void gifImageContentToUpload(OutputStream out, String imgpath)
            throws WeiboException {
        StringBuilder temp = new StringBuilder();

        temp.append(MP_BOUNDARY).append("\r\n");
        temp.append("Content-Disposition: form-data; name=\"pic\"; filename=\"")
                .append("news_image").append("\"\r\n");
        String filetype = "image/gif";
        temp.append("Content-Type: ").append(filetype).append("\r\n\r\n");
        byte[] res = temp.toString().getBytes();
		BufferedInputStream bis = null;
        
        int PKGLENGTH = 1024*1024*2; //2M
        byte[] data = new byte[PKGLENGTH];
        File obfile = new File(imgpath);//
        BufferedInputStream instream = null;
        
        try {
            out.write(res);
            
//            imgpath.compress(CompressFormat.PNG, 75, out);
            instream = new BufferedInputStream(new FileInputStream(obfile));
            instream.read(data, 0, PKGLENGTH);
            instream.close();
            out.write(data);
            
            out.write("\r\n".getBytes());
            out.write(("\r\n" + END_MP_BOUNDARY).getBytes());
        } catch (IOException e) {
            throw new WeiboException(e);
        } finally {
        	try {
				if (null != bis) bis.close();
        		if(instream != null) instream.close();
//				if(out != null) out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	
        }
    }


调用的地方就成了这样:

                if (!TextUtils.isEmpty(file)) {
                    Utility.paramToUpload(bos, params);
                    Log.d(TAG, "Content-Type--------image/gif" );
                    post.setHeader("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY);
                    
//                    Bitmap bf = BitmapFactory.decodeFile(file);
//                    Utility.imageContentToUpload(bos, bf); //By Liuzw for share gif pic
                    Utility.gifImageContentToUpload(bos, file); 

                }


这样以来gif图片暂时能分享成功了。

但是不确定什么时候就来个 service unavailable 的错误在分享的时候,

正解决中... ...


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值