Monday, 10 August 2015

Image and Text Sharing Using Share Intent

Hello Guys,

Today I am sharing a new topic in android, Its Image Sharing.

User can share their images, text, links, messages and so on things via Share Intent.

Its a very simple to implement in your android app.

Here I show you how to share image and how to show links.

Sample Code:

File: MainActivity.java

 /*
* Method for share either text or URL.
*/
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, "My Link");
share.putExtra(Intent.EXTRA_TEXT, "http://creativeandroidguide.blogspot.in/");

startActivity(Intent.createChooser(share, "Share Link!"));
}

/*
* Method for share any image.
*/
private void shareImage()
{
int imagid = R.drawable.ic_launcher;
            Log.i("Log_Image_Id", "Selected image is: "+imagid);
   
                String _ImageFile = "android.resource://" +                                                                                             getResources().getResourceName(imagid).replace(":", "/");
                Log.i("Log_ImageFile", "ImageFile is: "+_ImageFile);
        
                Uri imageUri = Uri.parse(_ImageFile);
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("image/*");
                intent.putExtra(Intent.EXTRA_STREAM, imageUri);
                startActivity(Intent.createChooser(intent, "Share Image"));
}


Output:


























Download Source Code:  AndroidShareIntent

No comments:

Post a Comment