在工作中碰到需要分享9张图片到微信朋友圈,在网上苦苦搜寻,找了ShareSDK,百度社会化组件,微信官方SDK之类的,通通不能得以解决,后来无意中看到知乎上某个高人的答复,终于实现了,实现代码如下:
Intent intent = new Intent(); ComponentName comp = new ComponentName("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 = new ArrayList<Uri>(); for (File f : files) { imageUris.add(Uri.fromFile(f)); } intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris); startActivity(intent);
由于我发送的是网络上获取的图片,为了实现分享,我用了一个颇为复杂的办法,就是把获取到的图片存储在本地,然后再得到图片的file地址,再进行分享:
for (int i = 0; i < lenght; i++) { File file = saveImageToSdCard(images.get(i)); files.add(file); F.makeLog(file.toString()); }
public static final File saveImageToSdCard(ImageView image) { boolean success = false; // F.makeLog(image.toString()); // Encode the file as a PNG image. File file = null; try { // file = createImageFile(); file = createStableImageFile(); } catch (IOException e1) { e1.printStackTrace(); } BitmapDrawable drawable = (BitmapDrawable) image.getDrawable(); Bitmap bitmap = drawable.getBitmap(); FileOutputStream outStream; try { outStream = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); // 100 to keep full quality of the image outStream.flush(); outStream.close(); success = true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (success) { // Toast.makeText(getApplicationContext(), "Image saved with success", // Toast.LENGTH_LONG).show(); return file; } else { return null; } }
public static File createStableImageFile() throws IOException { IMAGE_NAME++; String imageFileName = Integer.toString(IMAGE_NAME) + ".jpg"; File storageDir = AppData.getContext().getExternalCacheDir(); // File image = File.createTempFile( // imageFileName, /* prefix */ // ".jpg", /* suffix */ // storageDir /* directory */ // ); File image = new File(storageDir, imageFileName); // Save a file: path for use with ACTION_VIEW intents // mCurrentPhotoPath = "file:" + image.getAbsolutePath(); return image; }
最后在艹下GFW,又把google屏蔽了,让我们这些底层码农该怎么办,我只是老老实实混口饭吃,都要破坏。