Android 微信網址分享添加網絡圖片

public static void share(String CustomEventData,String title,String titlle_detail,String imgUrl)throwsMalformedURLException{

        Log.e(TAG,"share##########################");

        Log.e(TAG,imgUrl);

        WXWebpageObject webpage=newWXWebpageObject();

        webpage.webpageUrl="https://fir.im/5et2";

        WXMediaMessage msg=newWXMediaMessage(webpage);

        msg.title=title;

        msg.description=titlle_detail;


//加載本地圖片

//        Bitmap thumb = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.zhuye_tip);

//        msg.setThumbImage(thumb);

//        thumb.recycle();


//加載網絡圖片********

//注意下方的壓縮

        try{

        Bitmap thumb=BitmapFactory.decodeStream(new URL(imgUrl).openStream());

//注意下面的這句壓縮,120,150是長寬。

//必定要壓縮,否則會分享失敗

        Bitmap thumbBmp=Bitmap.createScaledBitmap(thumb,120,150,true);

//Bitmap回收

        thumb.recycle();

        msg.thumbData=Util.bmpToByteArray(thumbBmp,true);

//            msg.setThumbImage(thumb);

        }catch(IOException e){

        e.printStackTrace();

        }

        Log.e(TAG,msg.title);

        Log.e(TAG,msg.description);

//構造Req

        SendMessageToWX.Req req=newSendMessageToWX.Req();

        req.transaction=buildTransaction("webpage");

        req.message=msg;

        Log.e(TAG,CustomEventData);

        if(Integer.parseInt(CustomEventData)==0){

        req.scene=SendMessageToWX.Req.WXSceneSession;

        }

        else{

        req.scene=SendMessageToWX.Req.WXSceneTimeline;

        }

        api.sendReq(req);//發送到微信

        Log.e(TAG,"share###### END ####################");

        }
相關文章
相關標籤/搜索