分享多张图片到微信朋友圈

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/YANGWEIQIAO/article/details/53127450

实现代码如下:


[java]  view plain  copy
  1. Intent intent = new Intent();  
  2.         ComponentName comp = new ComponentName("com.tencent.mm",  
  3.                 "com.tencent.mm.ui.tools.ShareToTimeLineUI");  
  4.         intent.setComponent(comp);  
  5.         intent.setAction(Intent.ACTION_SEND_MULTIPLE);  
  6.         intent.setType("image/*");  
  7.         intent.putExtra("Kdescription", title);  
  8.         ArrayList<Uri> imageUris = new ArrayList<Uri>();  
  9.         for (File f : files) {  
  10.             imageUris.add(Uri.fromFile(f));  
  11.         }  
  12.         intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);  
  13.         startActivity(intent);  

就这么简单的几个代码,而且也不用向微信申请Key,测底解决,唯一的缺陷就是不能够实现回调


由于我发送的是网络上获取的图片,为了实现分享,我用了一个颇为复杂的办法,就是把获取到的图片存储在本地,然后再得到图片的file地址,再进行分享:


[java]  view plain  copy
  1. for (int i = 0; i < lenght; i++) {  
  2.             File file = saveImageToSdCard(images.get(i));  
  3.             files.add(file);  
  4.             F.makeLog(file.toString());  
  5.         }  

[java]  view plain  copy
  1. public static final File saveImageToSdCard(ImageView image) {  
  2.         boolean success = false;  
  3. //      F.makeLog(image.toString());  
  4.         // Encode the file as a PNG image.  
  5.         File file = null;  
  6.         try {  
  7. //          file = createImageFile();  
  8.             file = createStableImageFile();  
  9.         } catch (IOException e1) {  
  10.             e1.printStackTrace();  
  11.         }  
  12.         BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();  
  13.         Bitmap bitmap = drawable.getBitmap();  
  14.         FileOutputStream outStream;  
  15.         try {  
  16.             outStream = new FileOutputStream(file);  
  17.             bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);  
  18. //           100 to keep full quality of the image   
  19.             outStream.flush();  
  20.             outStream.close();  
  21.             success = true;  
  22.         } catch (FileNotFoundException e) {  
  23.             e.printStackTrace();  
  24.         } catch (IOException e) {  
  25.             e.printStackTrace();  
  26.         }  
  27.         if (success) {  
  28. //          Toast.makeText(getApplicationContext(), "Image saved with success",  
  29. //                  Toast.LENGTH_LONG).show();  
  30.             return file;  
  31.         } else {  
  32.                     return null;  
  33.         }  
  34.     }  


其中在获取图片地址的时候,如果每次的地址都是新生成的话,那么分享几次,存储空间就都塞满图片了,所以我设置了9个固定的地址,每次有新的分享,就覆盖之前的图片,地址获取代码如下:

[java]  view plain  copy
  1. public static File createStableImageFile() throws IOException {  
  2.         IMAGE_NAME++;  
  3.         String imageFileName = Integer.toString(IMAGE_NAME) + ".jpg";  
  4.         File storageDir = AppData.getContext().getExternalCacheDir();  
  5. //        File image = File.createTempFile(  
  6. //            imageFileName,  /* prefix */  
  7. //            ".jpg",         /* suffix */  
  8. //            storageDir      /* directory */  
  9. //        );  
  10.         File image = new File(storageDir, imageFileName);  
  11.   
  12.         // Save a file: path for use with ACTION_VIEW intents  
  13. //        mCurrentPhotoPath = "file:" + image.getAbsolutePath();  
  14.         return image;  
  15.     }  

然后在每次分享完后,重置IMAGE_NAME的值, 该方法由于需要先把图片存储在本地,所以颇为费时,不知各位可有其他的实现方式??

猜你喜欢

转载自blog.csdn.net/YANGWEIQIAO/article/details/53127450