android - 如何将多张图片分享到 whatsapp 应用程序?

标签 android image whatsapp android-sharing

如何将多个图像共享到所有共享应用程序,例如(WhatsApp、电子邮件)但是当我尝试将多个图像共享到 WhatsApp 时,我收到消息:共享失败,请重试。我更改了 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 上共享。

请帮忙。

最佳答案

与 Android 上的大多数社交应用程序一样,WhatsApp 会聆听共享媒体和文本的 Intent 。例如,只需创建一个共享文本的 Intent ,系统选择器就会显示 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 并绕过系统选择器,您可以通过在您的 Intent 中使用 setPackage 来实现:

sendIntent.setPackage("com.whatsapp");

这将在您调用 startActivity(sendIntent) 之前简单地设置;

关于android - 如何将多张图片分享到 whatsapp 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51239091/

相关文章:

image - 两幅图像的互信息和联合熵-MATLAB

html - 在图像上叠加三个不同的图像,只先叠加

python - PyQt:将缩放图像放在标签的中心

android - 如何以编程方式删除正在进行的通知?

swift - 无法在 swift 中共享 pdf(仅限 WhatsApp)

android - Facebook FriendPickerSample android 项目不显示好友列表

android - 如何删除默认工具栏菜单项并替换为不同的图标?

java - 判断是否来自Android App的HTTP请求?然后适当回应

c# - 400 错误请求仅在 Android 应用程序中无效的主机名

onclick - 如何使用 +55 (51) 9988-7766 等号码在 iPhone 或 Android 中点击打开 WhatsApp