android 項目 分享圖片到微信

來源:互聯網
上載者:User

android 項目 分享圖片到

慕課網學習項目

android 項目 分享圖片到

AlertDialog 對話方塊設定

.9.png的編輯及使用

sdk->tools->draw9patch.bat

API的調用

package com.dongni.wxshare;import com.tencent.mm.sdk.modelmsg.SendMessageToWX;import com.tencent.mm.sdk.modelmsg.WXImageObject;import com.tencent.mm.sdk.modelmsg.WXMediaMessage;import com.tencent.mm.sdk.modelmsg.WXWebpageObject;import com.tencent.mm.sdk.openapi.IWXAPI;import com.tencent.mm.sdk.openapi.WXAPIFactory;import android.support.v7.app.ActionBarActivity;import android.app.AlertDialog;import android.content.DialogInterface;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.Typeface;import android.os.Bundle;import android.provider.MediaStore;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.Window;import android.view.WindowManager;import android.widget.*;public class MainActivity extends ActionBarActivity {    private ImageView mPhoto;    private EditText mWord;    private Button shareBtn;    private int flag=-1;    private IWXAPI iwxapi;        private Bitmap getPic()    {        shareBtn.setVisibility(View.INVISIBLE);        View view =getWindow().getDecorView();        view.setDrawingCacheEnabled(true);        view.buildDrawingCache();        return view.getDrawingCache();    }    private void wxshare(int flag) {        // TODO Auto-generated method stub        WXWebpageObject webpage=new WXWebpageObject();        WXMediaMessage msg=new WXMediaMessage();        msg.mediaObject=new WXImageObject(getPic());                SendMessageToWX.Req req=new SendMessageToWX.Req();        req.transaction=String.valueOf(System.currentTimeMillis());        req.message=msg;        req.scene=flag==0?SendMessageToWX.Req.WXSceneSession:SendMessageToWX.Req.WXSceneTimeline;        //req.scene=SendMessageToWX.Req.WXSceneSession;        iwxapi.sendReq(req);    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉標題列       // this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉資訊列        setContentView(R.layout.activity_main);                iwxapi=WXAPIFactory.createWXAPI(this, "wx3507afc2c2b49ccf");   //appId        iwxapi.registerApp("wx3507afc2c2b49ccf");                mPhoto=(ImageView) findViewById(R.id.pic);        mWord=(EditText)findViewById(R.id.word);        shareBtn=(Button)findViewById(R.id.fxbtn);        mPhoto.setOnClickListener(new View.OnClickListener() {                        @Override            public void onClick(View arg0) {                // TODO Auto-generated method stub                Intent intent =new Intent(Intent.ACTION_PICK,null);                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");                startActivityForResult(intent,100);            }        });                shareBtn.setOnClickListener(new View.OnClickListener(){            @Override            public void onClick(View arg0) {                // TODO Auto-generated method stub                final String[] item=new String[]{"分享給好友","分享到朋友圈"};                new AlertDialog.Builder(MainActivity.this)                 .setTitle("分享到")                //.setMessage("分享到哪?")                .setItems(item, new DialogInterface.OnClickListener() {                                        @Override                    public void onClick(DialogInterface arg0, int witch) {                        // TODO Auto-generated method stub                        flag=witch;                    }                })                .setNegativeButton("取消", new DialogInterface.OnClickListener() {                                        @Override                    public void onClick(DialogInterface arg0, int arg1) {                        // TODO Auto-generated method stub                        flag=-1;                    }                })                                .show();                if(flag>=0){                    wxshare(flag);                                        shareBtn.setVisibility(View.VISIBLE);                }                            }        });        mWord.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/hksn.ttf"));    }    @Override    protected void onActivityResult(int requestCode,int resultCode,Intent data)    {        super.onActivityResult(requestCode, resultCode, data);        if(resultCode==RESULT_OK&&requestCode==100){            if(data!=null){                mPhoto.setImageURI(data.getData());            }        }    }        @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.