0

I have to attach an image file with the sms on button click i use this code

final Intent smsIntent = new Intent(android.content.Intent.ACTION_SEND);
smsIntent.putExtra("sms_body", "Hello World!"); 
smsIntent.putExtra("address", "0123456789");
smsIntent.putExtra(Intent.ACTION_ATTACH_DATA,screenshotUri);
smsIntent.setType("image/png");
startActivity(smsIntent);

But this shows a chooser to choose an action like Facebook,Email,Messages etc. But i do not want any the chosser view it will directly show the message intent with attached file.

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
Amandeep singh
  • 1,865
  • 7
  • 21
  • 41

1 Answers1

2

What you are looking for cannot be done using implicit Intents. Because that is how the Android system is designed to handle implicit intents. If you want your intent to be handled by a specific application then you have to make them explicit, i.e., specify a component that has to handle the intent. But when you use explicit intents to handle any situation such as yours, then there is a great chance of your app to break when the specific component (i.e. Application) doesn't not exist in the target device. Android is being adopted by several OEMs, therefore each of them tend to replace the stock Messaging application with their own. So what seems to work on one device may not work on the other.

If you want to achieve what you want then you may have to get the list of Messaging applications on various devices, (you can find the stock Android app's component name from the emulator itself). And use the PackageManager to find if the component exists. If it does, the start and explicit intent, in which you won't receive an IntentChooser. If the component doesn't exist send an implicit intent.

You can learn more about intents from here.

Determining if an Activity exists on the current device? - This post will help you to find if the target component exists.

Community
  • 1
  • 1
Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155