android 项目 分享图片到微信

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 项目 分享图片到微信相关的知识,希望对你有一定的参考价值。

慕课网学习项目

android 项目 分享图片到微信

技术分享

 

AlertDialog 对话框设置

 http://www.cnblogs.com/tyks/p/5202812.html

 

.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);
    }
}
View Code

 

以上是关于android 项目 分享图片到微信的主要内容,如果未能解决你的问题,请参考以下文章

安卓,调用系统方法分享到微信

Android 微信 图片无法分享

android 实现微信分享多张图片的功能

如何把app分享到微信

Android分享图片和视频失败,怎么办?

为啥苹果手机截图不能分享到微信?