1. 程式人生 > >android平臺 新浪微博開發 分享gif不能動的問題原因

android平臺 新浪微博開發 分享gif不能動的問題原因

在新浪文件中明明指明

pic true binary 要上傳的圖片,僅支援JPEG、GIF、PNG格式,圖片大小小於5M。

支援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 的錯誤在分享的時候,

正解決中... ...