0

I would like to send a Bitmap and some text by clicking on a button in a fragment and I would like to let the user choose the contact/number in his messaging app.

How I could do that ?

EDIT :

After trying something, I got an error in the message app (Messenger on the emulator) :

Messenger failed to load attachment.

Here is the code I used :

String path = Environment.getExternalStorageDirectory().getPath() + "/Improov/LatestShare.png";
File file = new File(path);
FileOutputStream out = null;
try {
    out = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
        if (out != null) {
            out.close();
        }
    } catch (IOException e) {
         e.printStackTrace();
    }
}
Uri photoURI = FileProvider.getUriForFile(getContext(), getContext().getApplicationContext().getPackageName() + ".com.example.mous.improov_flash", file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("sms_body", message);
intent.putExtra(Intent.EXTRA_STREAM, photoURI);
intent.setType("image/jpeg");
getActivity().startActivity(intent);
Yohann L.
  • 1,262
  • 13
  • 27
  • This is rather broad. What particular aspect of the problem do you want this question to focus on? The button? The fragment? The text? The `Bitmap`? The user choosing the contact? Getting the phone number of the contact? Sending an MMS? – CommonsWare Aug 01 '17 at 20:33
  • @CommonsWare well, I guess using an intent to launch the sms activity with a pre-populated sms/mms with the bitmap I want to send – Yohann L. Aug 01 '17 at 20:35

1 Answers1

0

If you like to send MMS,

First, you should store your bitmap in sd card, then use the following intent to send the MMS,

Get the Uri for your stored bitmap

Uri uri = [uri for your stored bitmap];

Intent intent = new Intent(Intent.ACTION_SEND);
inetnt.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra("sms_body", [Text to be send]);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/jpeg");
getActivity.startActivity(intent);

For more detail read this official doc

Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
  • Thank you but I got an error when I launch the messenger app, I edited my post with the code and the message I have from Messenger – Yohann L. Aug 01 '17 at 21:17
  • check for storing bitmap into external storage you have runtime permission or not, and you crash log in your question – Muthukrishnan Rajendran Aug 01 '17 at 21:21
  • I used FileProvider because of the runtime permission. First I had the error `FileUriExposedException`, that's why I used FileProvider, I find the solution using FileProvider [here](https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed) – Yohann L. Aug 01 '17 at 21:26
  • If you have crash, then add your crash log in your question – Muthukrishnan Rajendran Aug 01 '17 at 21:27
  • No it's not a crash, there is no error in the log, it's just a Toast message from the application Messenger saying _Messenger failed to load attachment._ – Yohann L. Aug 01 '17 at 21:34
  • Add **inetnt.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);** also – Muthukrishnan Rajendran Aug 01 '17 at 21:39
  • Still not working, I have the same Toast message. I also tried in different app on my phone but it's the same, the app can't open the attached file – Yohann L. Aug 01 '17 at 21:45