Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> android 項目 分享圖片到微信

android 項目 分享圖片到微信

編輯:關於android開發

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);
    }
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved