Android - wechat share 微信分享
Intent:
Intent intent = new Intent();
//選擇好友分享頁面
ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
intent.setComponent(comp);
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putExtra("Kdescription", "我在測試");
intent.setType("image/*");
//添加Uri圖片
ArrayList<Uri> imageUris = new ArrayList<Uri>();
imageUris.add(getScreenShotTop());
ntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
startActivity(intent);
SDK:(compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:1.1.2')
//分享
String text = "測試分享文字";
// 初始化一个WXTextObject对象
WXTextObject textObj = new WXTextObject();
textObj.text = text;
// 用WXTextObject对象初始化一个WXMediaMessage对象
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = textObj;
// 发送文本类型的消息时,title字段不起作用
// msg.title = "Will be ignored";
msg.description = text;
// 构造一个Req
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("text"); // transaction字段用于唯一标识一个请求
req.message = msg;
SendMessageToWX.Req.WXSceneSession;
req.scene = SendMessageToWX.Req.WXSceneTimeline;//分享至好友圈
req.scene = SendMessageToWX.Req.WXSceneSession;//分享至特定好友對話
// 调用api接口发送数据到微信
api.sendReq(req);
截圖:
//將全螢幕畫面轉換成Bitmap
private Uri getScreenShotTop() {
//藉由View來Cache全螢幕畫面後放入Bitmap
View mView = getWindow().getDecorView();
mView.setDrawingCacheEnabled(true);
mView.buildDrawingCache();
Bitmap mFullBitmap = mView.getDrawingCache();
//取得系統狀態列高度
Rect mRect = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(mRect);
int mStatusBarHeight = mRect.top;
//取得Title高
int mTitleHeight = 0;
//不要的地方
int mRemoveHeight = mStatusBarHeight + mTitleHeight;
//取得手機螢幕長寬尺寸
int mPhoneWidth = getWindowManager().getDefaultDisplay().getWidth();
int mPhoneHeight = getWindowManager().getDefaultDisplay().getHeight();
int hh = (mPhoneHeight - mRemoveHeight);
//將狀態列的部分移除並建立新的Bitmap
Bitmap mBitmap = Bitmap.createBitmap(mFullBitmap, 0, mRemoveHeight, mPhoneWidth, hh);
//將Cache的畫面清除
mView.destroyDrawingCache();
// imageView.setImageBitmap(bm);
String date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new java.util.Date());
File file = new File(new Config().rootDir + date + ".png");
if (file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
FileOutputStream fos = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Uri.fromFile(file);
}
private Uri getScreenShotTop() {
//藉由View來Cache全螢幕畫面後放入Bitmap
View mView = getWindow().getDecorView();
mView.setDrawingCacheEnabled(true);
mView.buildDrawingCache();
Bitmap mFullBitmap = mView.getDrawingCache();
//取得系統狀態列高度
Rect mRect = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(mRect);
int mStatusBarHeight = mRect.top;
//取得Title高
int mTitleHeight = 0;
//不要的地方
int mRemoveHeight = mStatusBarHeight + mTitleHeight;
//取得手機螢幕長寬尺寸
int mPhoneWidth = getWindowManager().getDefaultDisplay().getWidth();
int mPhoneHeight = getWindowManager().getDefaultDisplay().getHeight();
int hh = (mPhoneHeight - mRemoveHeight);
//將狀態列的部分移除並建立新的Bitmap
Bitmap mBitmap = Bitmap.createBitmap(mFullBitmap, 0, mRemoveHeight, mPhoneWidth, hh);
//將Cache的畫面清除
mView.destroyDrawingCache();
// imageView.setImageBitmap(bm);
String date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new java.util.Date());
File file = new File(new Config().rootDir + date + ".png");
if (file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
FileOutputStream fos = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Uri.fromFile(file);
}
留言
張貼留言