如何与whatsapp应用共享多张图片?

时间:2018-07-09 06:05:53

标签: android image whatsapp android-sharing

如何将多个图像共享到所有共享应用程序(例如WhatsApp,电子邮件),但是当我尝试将多个图像共享到WhatsApp时,消息为sharing failed, please try again。我更改了setType,但无法正常工作 请检查此链接

  

https://trinitytuts.com/share-multiple-images-to-whatsapp-or-another-app-android/

请检查以下代码:

imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //imageUriArray = new ArrayList<Uri>();
            toolbar.setVisibility(View.GONE);

            for (int i = 0; i < arrayList.size(); i++) {
                Date now = new Date();
                android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
                aa = screenWidth * i;
                horizontal.scrollTo(aa, 0);


                try {
                    // image naming and path  to include sd card  appending name you choose for file
                    String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + i + ".jpg";

                    // create bitmap screen capture
                    View v1 = myView.getRootView();
                    v1.setDrawingCacheEnabled(true);
                    Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
                    v1.setDrawingCacheEnabled(false);

                    File imageFile = new File(mPath);

                    FileOutputStream outputStream = new FileOutputStream(imageFile);
                    int quality = 100;
                    bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
                    outputStream.flush();
                    outputStream.close();
                    imageUriArray.add(Uri.fromFile(new File(String.valueOf(imageFile))));
                    //openScreenshot(imageFile);
                } catch (Throwable e) {
                    // Several error may come out with file handling or OOM
                    e.printStackTrace();
                }
            }
            toolbar.setVisibility(View.VISIBLE);
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUriArray);
            intent.setType("image/jpeg");
            startActivity(intent);
        }
    });

    arrayList = new ArrayList<>();
    arrayList.add("https://trinitytuts.com/wp-content/uploads/2015/02/trinitylogo.png");
    arrayList.add("http://lh4.googleusercontent.com/-1hHevfC4VTQ/AAAAAAAAAAI/AAAAAAAAAGs/Bi_dipj31f4/photo.jpg?sz=104");

    for (int ii = 0; ii < arrayList.size(); ii++) {
        LayoutInflater inflaters = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        myView = inflaters.inflate(R.layout.pager_item_multi, null);
        layMain = (LinearLayout) myView.findViewById(R.id.layMain);
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        //int screenHeight = metrics.heightPixels;
        screenWidth = metrics.widthPixels;
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(screenWidth, LinearLayout.LayoutParams.MATCH_PARENT);
        ImageView imageViews = (ImageView) myView.findViewById(R.id.img_pager_item);
        Picasso.with(this).load(arrayList.get(ii)).placeholder(R.mipmap.ic_launcher).into(imageViews);
        layMain.setLayoutParams(layoutParams);
        layMain.setTag("" + ii);
        layout.addView(myView);
    }
}

我在URL中传递图像,然后单击按钮,所有图像都在WhatsApp上共享。

请帮助。

1 个答案:

答案 0 :(得分:0)

与Android上的大多数社交应用程序一样,WhatsApp会听取共享媒体和文本的意图。例如,只需创建一个共享文本的意图,系统选择器就会显示WhatsApp:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Your image url here.");
sendIntent.setType("text/plain");
startActivity(sendIntent);

但是,如果您希望直接与WhatsApp共享并绕过系统选择器,则可以通过使用setPackage来实现:

sendIntent.setPackage("com.whatsapp");

只需在调用startActivity(sendIntent);之前直接设置该值即可