Search code examples
androidintentfilter

Facing trouble while sharing text, link and images together using intent filter


I am implementing intent filter to share some kind of information. But it is having a problem.

btnShareLocInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Location Name:" + locName.getTitle());
                shareIntent.putExtra(Intent.EXTRA_TEXT, locName.getTitle());
                shareIntent.putExtra(Intent.EXTRA_TEXT, locName.getDescription());
                shareIntent.putExtra(Intent.EXTRA_TEXT, getLocUri().toString());
                shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse(getResources().getString(R.string.image_url) + locName.getImage1()));
                shareIntent.setType("image/*");
                shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                startActivity(Intent.createChooser(shareIntent, "Share via"));
            }
        });

Here it displays title in subject and link but doesn't display title, description in text body. Also on link click I would like to perform check if app is installed or not in phone, which doesn't work as well.

I have looked for similar problems but any of the solutions didn't work for me. Any help would be appreciated.


Solution

  • Bundle uses an ArrayMap inside for store info. If you use putExtra method more than one time with same key you replace the value. Form doc of ArrayMap:

    > /**
    >      * Add a new value to the array map.
    >      * @param key The key under which to store the value.  If
    >      * this key already exists in the array, its value will be replaced.
    >      * @param value The value to store for the given key.
    >      * @return Returns the old value that was stored for the given key, or null if there
    >      * was no such key.
    >      */
    

    So, concatenate info inside use putExtra for same key more than one time. Like this:

    String data = locName.getTitle() + locName.getDescription() + getLocUri().toString();
    
    shareIntent.putExtra(Intent.EXTRA_TEXT,data);
    

    Instead for checking if an app is installed you can use this method.

    public boolean isAppInstalled(Context context, String packageName) {
        try {
            context.getPackageManager().getApplicationInfo(packageName, 0);
            return true;
        }
        catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }
    

    and use it:

    isAppInstalled(myContext,"com.whatsapp"); // for checking whatsapp