Skip to main content

Android: Share Intent for Text,Image & URL

import java.io.File;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View.OnClickListener handler = new View.OnClickListener() {
            public void onClick(View v) {
                switch (v.getId()) {
                case R.id.buttonShareTextUrl:
                    shareTextUrl();
                    break;
                case R.id.buttonShareImage:
                    shareImage();
                    break;
                }
            }
        };     
        findViewById(R.id.buttonShareTextUrl).setOnClickListener(handler);
        findViewById(R.id.buttonShareImage).setOnClickListener(handler);

    }
    private void shareTextUrl() {
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("text/plain");
        share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        
        share.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post");
        share.putExtra(Intent.EXTRA_TEXT, "http://dhruvvaishnav.blogspot.in");

        startActivity(Intent.createChooser(share, "Share text to..."));
    }

    /*
     * Share any Image.
     */
    private void shareImage() {
        Intent share = new Intent(Intent.ACTION_SEND);
        // If you want to share a png image only, you can do:
        // setType("image/png"); OR for jpeg: setType("image/jpeg");
        share.setType("image/*");
        // Make sure you put example png image named myImage.png in your
        // directory
        String imagePath = Environment.getExternalStorageDirectory()
                + "/myImage.png";
        File imageFileToShare = new File(imagePath);
        Uri uri = Uri.fromFile(imageFileToShare);
        share.putExtra(Intent.EXTRA_STREAM, uri);
        startActivity(Intent.createChooser(share, "Share image to..."));
    }
}
Note: When you use the setType() method, you are enabling Android to filter what apps can share your content. For example, you are sharing a text or URL, the appropriate apps to be shown can be Facebook, Messaging or Email. If you are sharing an image, proper apps can be Instagram, or Picasa.

For More information follow this link : http://developer.android.com/training/sharing/send.html

Customizing the Intent extras depending on the target sharer:


private void shareItem(String title, String link) {
    // Standard message to send
    String msg = title + " " + link;

    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("text/plain");

    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
    if (!resInfo.isEmpty()) {
        List<Intent> targetedShareIntents = new ArrayList<Intent>();
        Intent targetedShareIntent = null;

        for (ResolveInfo resolveInfo : resInfo) {
            String packageName = resolveInfo.activityInfo.packageName;
            targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
            targetedShareIntent.setType("text/plain");

            // Find twitter: com.twitter.android...
            if ("com.twitter.android".equals(packageName)) {
                targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, msg);
            } else if ("com.google.android.gm".equals(packageName)) {
                targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, title);
                targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, Uri.encode(title + "\r\n" + link));
            } else if ("com.android.email".equals(packageName)) {
                targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, title);
                targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, Uri.encode(title + "\n" + link));
            } else {
                // Rest of Apps
                targetedShareIntent.putExtra( android.content.Intent.EXTRA_TEXT, msg);
            }

            targetedShareIntent.setPackage(packageName);
            targetedShareIntents.add(targetedShareIntent);
        }

        Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), getResources().getString(R.string.share));
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[] {}));
        startActivityForResult(chooserIntent, 0);
    }
}

Comments

  1. This information you provided in the blog that was really unique I love it!!, Thanks for sharing such a great blog..Keep posting..

    Andriod Training

    ReplyDelete
  2. This information you provided in the blog that was really unique I love it!!, Thanks for sharing such a great blog..Keep posting..

    Andriod Course in Chennai

    ReplyDelete
  3. i want to post both image and text in post

    ReplyDelete

Post a Comment

Popular posts from this blog

Error: Retrieving parent for item: No resource found that matches the given name after upgrading to AppCompat v23

My project is going on easily but suddenly what I found below bugs when developing an app. I know it's minor bug but it may be useful to anyone. Here is the error: Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button.Inverse'. Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.Button.Colored'. Solution: This happens because after updates your android studio uses API 23 by default. 1) First check that your compile SDK version must match the support library's major version. If you are using version 23 of the support library, you need to compile against version 23 of the Android SDK. Alternatively you can continue compiling against version 22 of the Android SDK by switching to the latest support library v22.   2) Go to your project structure -> Properties -> and change Build tool version to...

Be aware while Posting photo from a native ShareDialog in Facebook android SDK

Add the content provider to your manifest. <provider  android:name="com.facebook.NativeAppCallContentProvider" android:authorities="com.facebook.app.NativeAppCallContentProvider{Facebook-App-Id}" android:exported="true" /> Make sure exported=true (as above) Example: <provider android:name="com.facebook.NativeAppCallContentProvider" android:authorities="com.facebook.app.NativeAppCallContentProvider123456" android:exported="true" />

How to solve "com.google.android.gsf.login" has stopped problem ?

Suggestions 1) Remove any  accounts you activated then reconnect them again.. this should fIx and refresh accounts and services. Do factory reset if it does not work. 2) settings>applications>Google Play  and deleted data and cache. Required to log back into Google Play and all worked well from then on. When logged into GP noticed that your download is ready. Interesting thing is that when you started that download previously on your way BUT wifi on and the dwnld was interrupted. I think this may have caused the error. So delete data and cache and restart. 3) You can first try to go into the settings -> accounts and manually remove and reinstall your account to see if that corrects the issue. If it does not, then something got corrupted and you will need to reflash the ROM. All the tools you will need and the ROM is in the development section and ROM sections here 4) Deleting Your accounts in setting (Accounts and sync) then adding them back in. 5) F...