Ratri
Ratri

Reputation: 337

Open conversation based on clicked number then intent the image

I know this is a common question that has been asked. But my problem is different now. So I create a share button which if I click it will open list of phone number like below :

List of phone number

So when I click one of them, it will immediately open conversation in whats app base on the phone number I click. I use this code

 val url = "https://api.whatsapp.com/send?phone=62"+tempDatas!![position].custHpWa
                                val intent = Intent(Intent.ACTION_VIEW);
                                intent.putExtra(Intent.EXTRA_TEXT,intent.getStringExtra("DESCRIPTION") );
                                intent.data = Uri.parse(url)
                                startActivity(intent);

Then I add this code for intent image into the conversation

 rvListWa!!.addOnItemTouchListener(RecyclerItemClickListener(this@ShareFileActivity,
            RecyclerItemClickListener.OnItemClickListener { view, position ->

                Glide
                        .with(this@ShareFileActivity)
                        .load(baseURLPicasso+intent.getStringExtra("PICTURE"))
                        .asBitmap()
                        .into(object : SimpleTarget<Bitmap>() {
                            override fun onResourceReady(resource: Bitmap?, glideAnimation: GlideAnimation<in Bitmap>?) {

                                val url = "https://api.whatsapp.com/send?phone=62"+tempDatas!![position].custHpWa
                                val intent = Intent(Intent.ACTION_SEND);
                                intent.putExtra(Intent.EXTRA_TEXT,intent.getStringExtra("DESCRIPTION") );
                                val path = MediaStore.Images.Media.insertImage(getContentResolver(), resource, "", null);
                                val image = Uri.parse(path);

                                intent.data = Uri.parse(url)
                                intent.putExtra(Intent.EXTRA_STREAM, image);
                                intent.setType("image/*");
                                startActivity(intent);


                            }

                        })
            }))

Instead of open conversation base on number and send image. It turns out open whats app and choose to who we want to send it.

Is there any solution. Because I already try to make the Intent.ACTION_VIEW it will open gallery.

My image url is from database

Upvotes: 2

Views: 301

Answers (1)

Nagendra Hari Karthick
Nagendra Hari Karthick

Reputation: 483

Hope this works. Just pass the path the file location of the file variable and make sure you pass the correct phone number.

    val sendIntent = Intent("android.intent.action.SEND")
    val f = File("path to the file")
    val uri = Uri.fromFile(f)
    sendIntent.component = ComponentName("com.whatsapp", "com.whatsapp.ContactPicker")
    sendIntent.type = "image"
    sendIntent.putExtra(Intent.EXTRA_STREAM, uri)
    sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators("91**********") + "@s.whatsapp.net")
    sendIntent.putExtra(Intent.EXTRA_TEXT, "sample text you want to send along with the image")
    startActivity(sendIntent)

Upvotes: 1

Related Questions