这个我研究了好几天,微信文档跳着写的,还tm没技术支持,下面分享下我的心得,也算帮一下依然困在此的人 时间关系我没插图,见谅
1去【资源中心下载sdk】 (我们需要里面的jar包),和【签名生成工具】 移动应用抽屉里介入指南是文档 ,资源下载是 jar包 和签名工具
2.下载官方提供的Demo(需要复制里面的代码,demo导入eclips会报错不用管)
3.注册个微信开放平台的帐号,并且创建你们公司的app 跟着它的提示一步步走 注意看框下面的灰字提示
东西准备完 步骤开始
4.libammsdk.jar 这个jar包复制到项目 libs里,右键 Build Path --Configure Build Path..点进去 Libraries里有个 Add JARs 添加你的jar包
5.复制代码到你的事件里
需要分享的页面创建变量
private static final String APP_ID ="wxb8c2413759c";//ID是第3步创建应用成功后才有的
private IWXAPI api;
------------------------------------------
如点击事件里加入核心代码
regToWx();
WXWebpageObject webpage = new WXWebpageObject();
webpage.webpageUrl = "www.baidu.com";
WXMediaMessage msg = new WXMediaMessage(webpage);
msg.title = "WebPage";
msg.description = "WebPage";
Bitmap thumb = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
msg.thumbData = Util.bmpToByteArray(thumb, true);
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("webpage");
req.message = msg;
req.scene = SendMessageToWX.Req.WXSceneSession;
api.sendReq(req);
finish();
private void regToWx() {
api = WXAPIFactory.createWXAPI(this, APP_ID, false);
// 将应用注册到微信
api.registerApp(APP_ID);
}
还差一个工具类 代码太多了 起个名为Util 的工具类
private static final String TAG = "SDK_Sample.Util";
public static byte[] bmpToByteArray(final Bitmap bmp,
final boolean needRecycle) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 100, output);
if (needRecycle) {
bmp.recycle();
}
byte[] result = output.toByteArray();
try {
output.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static byte[] getHtmlByteArray(final String url) {
URL htmlUrl = null;
InputStream inStream = null;
try {
htmlUrl = new URL(url);
URLConnection connection = htmlUrl.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
inStream = httpConnection.getInputStream();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
byte[] data = inputStreamToByte(inStream);
return data;
}
public static byte[] inputStreamToByte(InputStream is) {
try {
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
int ch;
while ((ch = is.read()) != -1) {
bytestream.write(ch);
}
byte imgdata[] = bytestream.toByteArray();
bytestream.close();
return imgdata;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static byte[] readFromFile(String fileName, int offset, int len) {
if (fileName == null) {
return null;
}
File file = new File(fileName);
if (!file.exists()) {
Log.i(TAG, "readFromFile: file not found");
return null;
}
if (len == -1) {
len = (int) file.length();
}
Log.d(TAG, "readFromFile : offset = " + offset + " len = " + len
+ " offset + len = " + (offset + len));
if (offset < 0) {
Log.e(TAG, "readFromFile invalid offset:" + offset);
return null;
}
if (len <= 0) {
Log.e(TAG, "readFromFile invalid len:" + len);
return null;
}
if (offset + len > (int) file.length()) {
Log.e(TAG, "readFromFile invalid file len:" + file.length());
return null;
}
byte[] b = null;
try {
RandomAccessFile in = new RandomAccessFile(fileName, "r");
b = new byte[len]; // ���������ļ���С������
in.seek(offset);
in.readFully(b);
in.close();
} catch (Exception e) {
Log.e(TAG, "readFromFile : errMsg = " + e.getMessage());
e.printStackTrace();
}
return b;
}
private static final int MAX_DECODE_PICTURE_SIZE = 1920 * 1440;
public static Bitmap extractThumbNail(final String path, final int height,
final int width, final boolean crop) {
Assert.assertTrue(path != null && !path.equals("") && height > 0
&& width > 0);
BitmapFactory.Options options = new BitmapFactory.Options();
try {
options.inJustDecodeBounds = true;
Bitmap tmp = BitmapFactory.decodeFile(path, options);
if (tmp != null) {
tmp.recycle();
tmp = null;
}
Log.d(TAG, "extractThumbNail: round=" + width + "x" + height
+ ", crop=" + crop);
final double beY = options.outHeight * 1.0 / height;
final double beX = options.outWidth * 1.0 / width;
Log.d(TAG, "extractThumbNail: extract beX = " + beX + ", beY = "
+ beY);
options.inSampleSize = (int) (crop ? (beY > beX ? beX : beY)
: (beY < beX ? beX : beY));
if (options.inSampleSize <= 1) {
options.inSampleSize = 1;
}
// NOTE: out of memory error
while (options.outHeight * options.outWidth / options.inSampleSize > MAX_DECODE_PICTURE_SIZE) {
options.inSampleSize++;
}
int newHeight = height;
int newWidth = width;
if (crop) {
if (beY > beX) {
newHeight = (int) (newWidth * 1.0 * options.outHeight / options.outWidth);
} else {
newWidth = (int) (newHeight * 1.0 * options.outWidth / options.outHeight);
}
} else {
if (beY < beX) {
newHeight = (int) (newWidth * 1.0 * options.outHeight / options.outWidth);
} else {
newWidth = (int) (newHeight * 1.0 * options.outWidth / options.outHeight);
}
}
options.inJustDecodeBounds = false;
Log.i(TAG, "bitmap required size=" + newWidth + "x" + newHeight
+ ", orig=" + options.outWidth + "x" + options.outHeight
+ ", sample=" + options.inSampleSize);
Bitmap bm = BitmapFactory.decodeFile(path, options);
if (bm == null) {
Log.e(TAG, "bitmap decode failed");
return null;
}
Log.i(TAG,
"bitmap decoded size=" + bm.getWidth() + "x"
+ bm.getHeight());
final Bitmap scale = Bitmap.createScaledBitmap(bm, newWidth,
newHeight, true);
if (scale != null) {
bm.recycle();
bm = scale;
}
if (crop) {
final Bitmap cropped = Bitmap.createBitmap(bm,
(bm.getWidth() - width) >> 1,
(bm.getHeight() - height) >> 1, width, height);
if (cropped == null) {
return bm;
}
bm.recycle();
bm = cropped;
Log.i(TAG,
"bitmap croped size=" + bm.getWidth() + "x"
+ bm.getHeight());
}
return bm;
} catch (final OutOfMemoryError e) {
Log.e(TAG, "decode bitmap failed: " + e.getMessage());
options = null;
}
return null;
}
工具类尾部
-------------------------------------------------------------------
6.清单文件里加入权限 官方文档有 注意放权限的位置和receiver的位置
<!-- 微信分享权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 微信分享权限 -->
<!--微信服务注册 -->
<receiver
android:name=".AppRegister"
android:permission="com.tencent.mm.plugin.permission.SEND" >
<intent-filter>
<action android:name="com.tencent.mm.plugin.openapi.Intent.ACTION_REFRESH_WXAPP" />
</intent-filter>
</receiver>
<!--微信服务注册 -->
7.去微信开放平台--管理中心-创建应用-- 一般两天就审核通过了
需要注意几个地方 1 包名一定写你项目的 2.签名是应用审核通过后,去签名生成工具输入你的包名后给你生成的绿色签名码 添加到你开放平台--管理中心的应用签名处
最后一步 去应用后台把官方给的APP_ID复制到你的代码里 应该就可以了,今天是周一 我周五没实现就差个小步骤,以上全是凭印象写的 我现在能实现指定分享给好友,到朋友圈应该就改个参数,具体去官方deno里看下,只能帮这么多了