Share image and text through Whatsapp or Facebook

前端 未结 12 906
谎友^
谎友^ 2020-11-28 05:23

I have in my app a share button and i want to share an image and a text at the same time. In GMail it works fine but in WhatsApp, only the image is sent and in Facebook the

12条回答
  •  遥遥无期
    2020-11-28 05:51

    public void shareIntentSpecificApps(String articleName, String articleContent, String imageURL) {
        List intentShareList = new ArrayList();
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        //shareIntent.setType("image/*");
        List resolveInfoList = getPackageManager().queryIntentActivities(shareIntent, 0);
    
        for (ResolveInfo resInfo : resolveInfoList) {
            String packageName = resInfo.activityInfo.packageName;
            String name = resInfo.activityInfo.name;
            Log.d("System Out", "Package Name : " + packageName);
            Log.d("System Out", "Name : " + name);
    
            if (packageName.contains("com.facebook") ||
                    packageName.contains("com.whatsapp")) {
    
    
                Intent intent = new Intent();
                intent.setComponent(new ComponentName(packageName, name));
                intent.setAction(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_SUBJECT, articleName);
                intent.putExtra(Intent.EXTRA_TEXT, articleName + "\n" + articleContent);
                Drawable dr = ivArticleImage.getDrawable();
                Bitmap bmp = ((GlideBitmapDrawable) dr.getCurrent()).getBitmap();
                intent.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(bmp));
                intent.setType("image/*");
                shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intentShareList.add(intent);
            }
        }
    
        if (intentShareList.isEmpty()) {
            Toast.makeText(ArticleDetailsActivity.this, "No apps to share !", Toast.LENGTH_SHORT).show();
        } else {
            Intent chooserIntent = Intent.createChooser(intentShareList.remove(0), "Share Articles");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentShareList.toArray(new Parcelable[]{}));
            startActivity(chooserIntent);
        }
    }
    

    You can share image also I have done in my app like mentioned in above code.

提交回复
热议问题