Android 微信分享 多张图片

备注:

①.这个不用导入微信的jar和注册微信的APP_ID 。
②. 实际上分享时是调用系统本地的分享功能,所以分享的图片必须要保存到本地。
③. 保存到本地是耗时操作 分享的代码需要在子线程执行 。

分享

1.分享到微信好友


// 图片地址List  
 final List urlList= new ArrayList<>();
// 图片File 
 final List files = new ArrayList<>();
 for (int i = 0; i < urlList.size(); i++) {
//网络图片保存到本地
  File file = Util.saveImageToSdCard(getContext(), urlList.get(i));
  files.add(file);
}
//分享的图片集合
ArrayList imageUris = new ArrayList();
 for (File f : files) {
 imageUris.add(Uri.fromFile(f));
}
//分享到微信好友
Intent intent = new Intent();
ComponentName componentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
if (imageUris.size() == 0) return;
intent.setComponent(componentName);
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, imageUris);
startActivity(intent);

2.分享到微信朋友圈


// 图片地址List  
 final List urlList= new ArrayList<>();
// 图片File 
 final List files = new ArrayList<>();
 for (int i = 0; i < urlList.size(); i++) {
//网络图片保存到本地
  File file = Util.saveImageToSdCard(getContext(), urlList.get(i));
  files.add(file);
}
//分享的图片集合
ArrayList imageUris = new ArrayList();
 for (File f : files) {
 imageUris.add(Uri.fromFile(f));
}
//分享到微信朋友圈
Intent weChatIntent = new Intent();
weChatIntent.setComponent(new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI"));
if (imageUris.size() == 0) return;
weChatIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
weChatIntent.setType("image/*");
weChatIntent.putExtra(Intent.EXTRA_STREAM, imageUris);
weChatIntent.putExtra("kdescription", data.get(i).Content.trim()); //分享描述
getContext().startActivity(weChatIntent);


补充 Util.saveImageToSdCard 代码



    public static String IMAGE_NAME = "iv_share";
    public static int  i =0;

    //根据网络图片url路径保存到本地
    public static final File saveImageToSdCard(Context context, String image) {
        boolean success = false;
        File file = null;
        try {
            file = createStableImageFile(context);
            Bitmap bitmap = null;
            URL url = new URL(image);
            HttpURLConnection conn = null;
            conn = (HttpURLConnection) url.openConnection();
            InputStream is = null;
            is = conn.getInputStream();
            bitmap =  BitmapFactory.decodeStream(is);
            FileOutputStream outStream;
            outStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            outStream.flush();
            outStream.close();
            success = true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (success) {
            return file;
        } else {
            return null;
        }
    }

    //创建本地保存路径
    public static File createStableImageFile(Context context) throws IOException {
        i++;
        String imageFileName =IMAGE_NAME + i+ ".jpg";
        File storageDir = context.getExternalCacheDir();
        File image = new File(storageDir, imageFileName);
        return image;
    }

你可能感兴趣的:(Android 微信分享 多张图片)