Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Share Image+Text using Share Intent in android

Tags:

android

I use this code, but its not working properly.

String Body = "Deals Name: " + Bussinessname + "\n"
    + "Deals: " + deal + "\n" + "Address: " + city
    + " ," + country;

Intent share = new Intent(android.content.Intent.ACTION_SEND);

Uri screenshotUri = Uri.parse(Images.Media.EXTERNAL_CONTENT_URI + "/" + image);
share.setType("image/*");
share.putExtra(Intent.EXTRA_SUBJECT, "The EssexPass");
share.putExtra(Intent.EXTRA_TEXT, Body);
share.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(share, "Share Deal"));

When I use the type text, then it works properly.
But I want to share a text and an image.
And the share intent is not working in FaceBook.

like image 524
Hardik Parmar Avatar asked Apr 28 '15 05:04

Hardik Parmar


People also ask

How do you send a picture text on Android?

Select the + icon, then choose a recipient or open an existing message thread. Select the + icon to add an attachment. Tap the Camera icon to take a picture, or tap the Gallery icon to browse for a photo to attach. Add text if desired, then tap the MMS button to send your image with your text message.


2 Answers

Try out the below code:

String fileName = "image-3116.jpg";//Name of an image
String externalStorageDirectory = Environment.getExternalStorageDirectory().toString();
String myDir = externalStorageDirectory + "/saved_images/"; // the file will be in saved_images
Uri uri = Uri.parse("file:///" + myDir + fileName);
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/html");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test Mail");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Launcher");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share Deal"));
like image 78
GrIsHu Avatar answered Oct 21 '22 18:10

GrIsHu


Using intent filter we are not able to share the both text + image on facebook. If you want to share the image and text together then you have to create the bitmap of image + text.

Download the source code here(Share image and text on facebook using intent in android)

MainActivity.java

package com.shareimage;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    EditText et_text;
    ImageView iv_image;
    TextView tv_share,tv_text;
    RelativeLayout rl_main;


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

        init();

    }

    private void init(){
        et_text = (EditText)findViewById(R.id.et_text);
        iv_image = (ImageView)findViewById(R.id.iv_image);
        tv_share = (TextView)findViewById(R.id.tv_share);
        rl_main = (RelativeLayout)findViewById(R.id.rl_main);
        tv_text= (TextView) findViewById(R.id.tv_text);

        File dir = new File("/sdcard/Testing/");
        try {
            if (dir.mkdir()) {
                System.out.println("Directory created");
            } else {
                System.out.println("Directory is not created");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        tv_share.setOnClickListener(this);

        et_text.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tv_text.setText(et_text.getText().toString());

            }
        });


    }




    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.tv_share:
                Bitmap bitmap1 = loadBitmapFromView(rl_main, rl_main.getWidth(), rl_main.getHeight());
                saveBitmap(bitmap1);
                String str_screenshot = "/sdcard/Testing/"+"testing" + ".jpg";

                fn_share(str_screenshot);
                break;
        }

    }

    public void saveBitmap(Bitmap bitmap) {
        File imagePath = new File("/sdcard/Testing/"+"testing" + ".jpg");
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(imagePath);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();

            Log.e("ImageSave", "Saveimage");
        } catch (FileNotFoundException e) {
            Log.e("GREC", e.getMessage(), e);
        } catch (IOException e) {
            Log.e("GREC", e.getMessage(), e);
        }
    }

    public static Bitmap loadBitmapFromView(View v, int width, int height) {
        Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        v.draw(c);

        return b;
    }

    public void fn_share(String path) {

        File file = new File("/mnt/" + path);

        Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath());
        Uri uri = Uri.fromFile(file);
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_STREAM, uri);

        startActivity(Intent.createChooser(intent, "Share Image"));


    }
}
like image 2
Deepshikha Puri Avatar answered Oct 21 '22 20:10

Deepshikha Puri