开发项目在遇到微信分享的时候,首先我们会去Share SDK里面吧代码集成好,关于微信,我们就必须去微信开放平台注册账号,审核项目,然后得到 APPID和APPSecret。
然后把从Share SDK里面集成过来的assets文件夹里面的ShareSDK.xml文件里面这三个关于微信的AppId和AppSecret替换掉。
这样就能使用分享到微信的功能啦。
然后就是分享的时候我们会从ShareSDK的官网集成下来代码 如下::
这里面的代码都是必须不能缺少的,但是图中标有红线的代码是必须要把本地的图片添加上去。如果没有这个图片那你分享到微信只会是一个文本(如下:),不会是一个链接。
正常的分享如下::
那图片就是我们项目的Logo图,所以保存SD很简单。
如下方法就是保存图片到SD卡中:
public void saveMyBitmap(String bitName,Bitmap mBitmap) {
File externalStorageDirectory = Environment.getExternalStorageDirectory();
File f = new File(externalStorageDirectory, bitName + ".png");
try {
f.createNewFile();
} catch (IOException e) {
System.out.println("在保存图片时出错:" + e.toString());
}
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
} catch (Exception e) {
//return "create_bitmap_error";
}
try {
fOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
到这里就会完事啦吗?想多啦,必须添加权限啊。。对于SD卡的读写操作去清单文件里面注册权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
好啦!!!!!我们还有一件事要做,对于Android6.0来说光在清单文件注册SD卡的权限是不行的吧,必须手动的添加权限,因为Android6.0的新特性,权限管理。如下:
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE };
/**
* Checks if the app has permission to write to device storage
* If the app does not has permission then the user will be prompted to
* grant permissions
* @param activity
*/
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE);
}
}
然后调用图中这个方法就大功告成啦!!!!
第一次写博客,写的不是很好,但是我会努力的,谢谢大家支持