在新浪文档中明明指明
pic | true | binary | 要上传的图片,仅支持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 的错误在分享的时候,
正解决中... ...