0

i want to send text and image both through intent but nothing is being sent, the code i have used is mentioned here; i am trying to send details of input and clicked image through intent; please tell me where it is going wrong and is there any other way of sending clicked image ?

MAINACTIVITY.JAVA:

public class MainActivity extends AppCompatActivity {
CheckBox cb1, cb2;
Button export, button;
//ImageView img;
Uri file;
EditText name, age, address, phone;
String genderr, namee, agee, addr, num;
private static final int CAMERA_REQUEST = 1888;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    export = findViewById(R.id.button);
    cb1 = findViewById(R.id.male);
    cb2 = findViewById(R.id.female);
    imageView = (ImageView) this.findViewById(R.id.imageView1);
    Button photoButton = (Button) this.findViewById(R.id.button1);
    photoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }
    });
    export.setOnClickListener(new View.OnClickListener()
    {
            @Override
            public void onClick(View view) {
                name = findViewById(R.id.et_name);
                name.setTypeface(null, Typeface.BOLD);
                age = findViewById(R.id.et_age);
                age.setTypeface(null, Typeface.BOLD);
                address = findViewById(R.id.address);
                phone = findViewById(R.id.phone);
                if (cb1.isChecked()) {
                    cb2.setChecked(false);
                    genderr = cb1.getText().toString();
                } else if (cb2.isChecked()) {
                    cb1.setChecked(false);
                    genderr = cb2.getText().toString();
                }

                final String content = "NAME: " + name.getText().toString() + "\n AGE: " + age.getText().toString() + "\n Gender: " + genderr + "\n ADDRESS: " + address.getText().toString() + "\n Phone: " + phone.getText().toString()+"\n IMAGE:";
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.setType("text/plain");
                sendIntent.putExtra(Intent.EXTRA_TEXT, content);
            sendIntent.putExtra(Intent.EXTRA_STREAM, (Parcelable) imageView);
              //  Intent sendIntent = new Intent(Intent.ACTION_PICK);
                sendIntent.setType("image/*");
              //  startActivityForResult(in, imageView);
                //  sendIntent.putExtra(Intent.EXTRA_TEXT, extraParams);
                startActivity(Intent.createChooser(sendIntent, "Send Using.."));
            }
        });
    }

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAMERA_REQUEST) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(photo);

ACTIVITY_MAIN:

  <ScrollView
    android:id="@+id/scrollView2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="1dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#72C8E8"
            android:padding="4sp"
            android:paddingLeft="4sp"
            android:paddingTop="4sp"
            android:paddingRight="4sp"
            android:paddingBottom="4sp"
            android:text="@string/name"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="30sp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/et_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName" />

        <TextView
            android:id="@+id/textView2"
            android:layout_marginTop="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#72C8E8"
            android:text="@string/age"
            android:padding="4sp"
            android:paddingLeft="4sp"
            android:paddingTop="4sp"
            android:paddingRight="4sp"
            android:paddingBottom="4sp"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="30sp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/et_age"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number" />

        <TextView
            android:id="@+id/textView3"
            android:layout_marginTop="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#72C8E8"
            android:padding="4sp"
            android:paddingLeft="4sp"
            android:paddingTop="4sp"
            android:paddingRight="4sp"
            android:paddingBottom="4sp"
            android:text="@string/gender"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="30sp"
            android:textStyle="bold" />

        <CheckBox
            android:id="@+id/male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:onClick="onCheckboxClicked"
            android:text="@string/m"
            android:textSize="24sp" />

        <CheckBox
            android:id="@+id/female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:onClick="onCheckboxClicked"
            android:text="@string/f"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="#72C8E8"
            android:padding="4sp"
            android:paddingLeft="4sp"
            android:paddingTop="4sp"
            android:paddingRight="4sp"
            android:paddingBottom="4sp"
            android:text="@string/address"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="30sp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPostalAddress" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="match_parent"
            android:layout_marginTop="20dp"
            android:layout_height="wrap_content"
            android:text="@string/phone"
            android:background="#72C8E8"
            android:padding="4sp"
            android:paddingLeft="4sp"
            android:paddingTop="4sp"
            android:paddingRight="4sp"
            android:paddingBottom="4sp"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="30sp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/camera"
            android:background="#72C8E8"
            android:padding="4sp"
            android:paddingLeft="4sp"
            android:paddingTop="4sp"
            android:paddingRight="4sp"
            android:paddingBottom="4sp"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="30sp"
            android:textStyle="bold"/>

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="281dp"
            android:layout_height="154dp"
            android:layout_above="@+id/button1"
            android:layout_alignParentTop="false"
            android:layout_gravity="center_horizontal"
            app:srcCompat="@drawable/ic_launcher_background" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="24dp"
            android:background="#72C8E8"
            android:padding="4sp"
            android:paddingLeft="4sp"
            android:paddingTop="4sp"
            android:paddingRight="4sp"
            android:paddingBottom="4sp"
            android:text="@string/img"
            android:textSize="24sp" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="24dp"
            android:background="#72C8E8"
            android:padding="4sp"
            android:paddingLeft="4sp"
            android:paddingTop="4sp"
            android:paddingRight="4sp"
            android:paddingBottom="4sp"
            android:text="@string/export"
            android:textSize="24sp" />

    </LinearLayout>
</ScrollView>

MANIFEST:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission-sdk-23 android:name="android.permission.CAMERA"/>
<uses-permission-sdk-23 android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-feature
    android:name="android.hardware.camera"
    android:required="false"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

  • Does this answer your question? [How to Share Image + Text together using ACTION\_SEND in android?](https://stackoverflow.com/questions/20333186/how-to-share-image-text-together-using-action-send-in-android) – Ravi Apr 15 '20 at 06:39
  • yes it partially does actually the problem is need to send the image just clicked through app itself –  Apr 15 '20 at 07:25
  • You need to get the URI of image and pass it via intent, – Ravi Apr 15 '20 at 11:13
  • Platform like Facebook accept only image when both image and text being send, so try on other app like whatsapp – Ravi Apr 15 '20 at 11:13
  • i have tried getting uri but while doing so it shows meta data doesnt exist can u help with how to get uri for the image –  Apr 15 '20 at 11:36

1 Answers1

0

In onActivityResult() check the request code and store bitmap to get its URI

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
        Uri imageUri = getImageUri(getApplicationContext(), photo);
        shareImageText(imageUri);
    }  
}

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

share image and text via intent like

private vois shareImageText(Uri uri)  {
    Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        intent.putExtra(Intent.EXTRA_TEXT,"Dummy Text");
        intent.setType("image/*");
        startActivity(Intent.createChooser(intent,"Share"));
}

Hope it will help.

Ravi
  • 2,277
  • 3
  • 22
  • 37
  • @ayushisinha have added it, kindly check – Ravi Apr 15 '20 at 12:51
  • hey in this the app crashes after image is clicked on device instead of image being displayed in app while on emulator it works fine –  Apr 17 '20 at 08:35
  • Bitmap photo = (Bitmap) data.getExtras().get("data"); here for getExtras() it shows error nullpointer exception so when i added object.requirenonull it needs minimum sdk 19 so if i make it 19, the app doesnt work on older versions of android –  Apr 17 '20 at 08:36
  • this is the error shown :E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NoClassDefFoundError: java.util.Objects on adding: @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { Bitmap photo = (Bitmap) (Objects.requireNonNull(data.getExtras())).get("data"); imageView.setImageBitmap(photo); –  Apr 17 '20 at 08:41
  • hey u have answered for thumbnail how do i send actual image through intent –  Apr 18 '20 at 07:55
  • Bitmap is 100% size of the image got capture, would recommend you to read camera documentation first – Ravi Apr 21 '20 at 07:04