最终的效果图,右下角微信多图为自定义调用系统分享,分享到微信。
在你能正常调用share sdk的时候想在原本的基础上加自定义的分享非常的简单。
它的官网已经给出了代码,但是给的不是很清楚。
lz毕竟是新手所以去问了客服(问第一个给我的是错误答案,坑了我。问了第二个才让我写了出来),下面直接上代码。
在源码里面有写好的添加方法,直接调用即可(在OnekeyShare类里)
在你调用分享的类里加上:
//自定义分享,微信多图分享Bitmap enableLogo = BitmapFactory.decodeResource(context.getResources(),R.mipmap.sharepic);Bitmap disableLogo = BitmapFactory.decodeResource(context.getResources(),R.mipmap.sharepic);String label = "微信多图";View.OnClickListener listener = newView.OnClickListener() {
public voidonClick(View v) {
}
};
上面的两个logo是显示的图片,label是下面的名字,再接着下面就是点击你加分享的点击事件了。
上面的写完调用添加自定义分享的方法:
oks.setCustomerLogo(enableLogo,disableLogo,label,listener);// 启动分享GUIoks.show(context);
oks是 OnekeyShare oks = new OnekeyShare;
这样子就可以在share sdk的原基础上完成你自定义添加的分享了。
下面说调用系统的分享,多图分享到微信:
这个是最终分享的效果图,你还可以自行的添加或删除分享的图片。
微信官网给出的分享只能分享一张图片,想要发多张(微信朋友圈最多只能发九张图片)怎么办呢?每个手机的系统分享可调微信多图分享。 好了不扯了,下面上代码:
private staticRunnable saveFileRunnable= newRunnable(){
@Overridepublic voidrun() {
try{
for(inti = 0;i < 9;i++) {
String[] ss = stringss[i].split("/");booleansaveTrue = MyTools.downloadLyWithName(AppStatic.Url_Base+stringss[i],ss[5],fileName,mContext);}
// 遍历 SD 卡下 .png 文件通过微信分享File file = newFile(Environment.getExternalStorageDirectory() + "/BangMai/images/"+ fileName);File[] files = file.listFiles(newFileFilter() {
@Overridepublic booleanaccept(File pathname) {
if(pathname.getName().endsWith(".jpg")) {
return true;}
return false;}
});Intent intent = newIntent();ComponentName comp = newComponentName("com.tencent.mm","com.tencent.mm.ui.tools.ShareToTimeLineUI");intent.setComponent(comp);intent.setAction(Intent.ACTION_SEND_MULTIPLE);intent.setType("image/*");ArrayList imageUris = newArrayList();for(File f : files) {
imageUris.add(Uri.fromFile(f));}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,imageUris);intent.putExtra("Kdescription","我分享成功啦!!!!!!");// 这里可要可不要,这句话的意思是直接会显示在发表时候的文字mContext.startActivity(intent);} catch(Exception e) {
e.printStackTrace();}
}
};
调用方法:
newThread(saveFileRunnable).start();
在你的OnClick事件里面加上这句就可以了。
因为是调用系统分享的方法,所以你要分享的图片必须要下载到本地分享的时候再从本地取出。try里面的第一个for是我自己写的下载图片的方法,你可以换成你自己写的。
这样子就可以在你的程序中调用系统的多图分享,分享到微信了。
最面是我的图片下载类:
// 保存带名称的图片public staticBoolean downloadLyWithName(String url,String imgName,String fileName,Context mContext) throwsException {
Bitmap bitmap1 = null;byte[] data1 = getImage(url);if(data1 != null) {
bitmap1 = BitmapFactory.decodeByteArray(data1,0,data1.length);// bitmapsaveImgWithName(bitmap1,imgName,fileName,mContext);return true;} elsereturn false;}
public static byte[] getImage(String path) throwsException {
URL url = newURL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(5* 1000);conn.setRequestMethod("GET");InputStream inStream = conn.getInputStream();if(conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
returnreadStream(inStream);}
return null;}
//保存图片带名称public static voidsaveImgWithName(Bitmap bitmap,String imgName,String fileName,Context mContext) {
if(bitmap != null) {
File appDir = newFile(Environment.getExternalStorageDirectory() + "/BangMai/");if(!appDir.exists()) {
appDir.mkdirs();}
if(fileName != null){
appDir = newFile(Environment.getExternalStorageDirectory() + "/BangMai/images/"+ fileName);if(!appDir.exists()) {
appDir.mkdirs();}
}
File file = null;file = newFile(appDir,imgName);try{
FileOutputStream fos = newFileOutputStream(file);if(null!= fos) {
bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos);fos.flush();fos.close();}
} catch(FileNotFoundException e) {
e.printStackTrace();} catch(IOException e) {
e.printStackTrace();}
// // 其次把文件插入到系统图库// try {// MediaStore.Images.Media.insertImage(mContext.getContentResolver(), file.getAbsolutePath(), imgName, null);// } catch (FileNotFoundException e) {// e.printStackTrace();// }// // 最后通知图库更新// mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/BangMai/")));}
}
end