失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 分享多张图片到微信朋友圈

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

时间:2023-09-28 12:41:51

相关推荐

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

实现代码如下:

[java]view plain copyIntentintent=newIntent(); ComponentNamecomp=newComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI"); intent.setComponent(comp); intent.setAction(Intent.ACTION_SEND_MULTIPLE); intent.setType("image/*"); intent.putExtra("Kdescription",title); ArrayList<Uri>imageUris=newArrayList<Uri>(); for(Filef:files){ imageUris.add(Uri.fromFile(f)); } intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,imageUris); startActivity(intent);

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

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

[java]view plain copyfor(inti=0;i<lenght;i++){ Filefile=saveImageToSdCard(images.get(i)); files.add(file); F.makeLog(file.toString()); }

[java]view plain copypublicstaticfinalFilesaveImageToSdCard(ImageViewimage){ booleansuccess=false; //F.makeLog(image.toString()); //EncodethefileasaPNGimage. Filefile=null; try{ //file=createImageFile(); file=createStableImageFile(); }catch(IOExceptione1){ e1.printStackTrace(); } BitmapDrawabledrawable=(BitmapDrawable)image.getDrawable(); Bitmapbitmap=drawable.getBitmap(); FileOutputStreamoutStream; try{ outStream=newFileOutputStream(file); press(pressFormat.PNG,100,outStream); //100tokeepfullqualityoftheimage outStream.flush(); outStream.close(); success=true; }catch(FileNotFoundExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); } if(success){ //Toast.makeText(getApplicationContext(),"Imagesavedwithsuccess", //Toast.LENGTH_LONG).show(); returnfile; }else{ returnnull; } }

其中在获取图片地址的时候,如果每次的地址都是新生成的话,那么分享几次,存储空间就都塞满图片了,所以我设置了9个固定的地址,每次有新的分享,就覆盖之前的图片,地址获取代码如下:[java]view plain copypublicstaticFilecreateStableImageFile()throwsIOException{ IMAGE_NAME++; StringimageFileName=Integer.toString(IMAGE_NAME)+".jpg"; FilestorageDir=AppData.getContext().getExternalCacheDir(); //Fileimage=File.createTempFile( //imageFileName,/*prefix*/ //".jpg",/*suffix*/ //storageDir/*directory*/ //); Fileimage=newFile(storageDir,imageFileName); //Saveafile:pathforusewithACTION_VIEWintents //mCurrentPhotoPath="file:"+image.getAbsolutePath(); returnimage; }

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

如果觉得《分享多张图片到微信朋友圈》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。