版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/YANGWEIQIAO/article/details/53127450
实现代码如下:
- 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);
就这么简单的几个代码,而且也不用向微信申请Key,测底解决,唯一的缺陷就是不能够实现回调
由于我发送的是网络上获取的图片,为了实现分享,我用了一个颇为复杂的办法,就是把获取到的图片存储在本地,然后再得到图片的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;
- }
然后在每次分享完后,重置IMAGE_NAME的值, 该方法由于需要先把图片存储在本地,所以颇为费时,不知各位可有其他的实现方式??