32

I'm unable to send MMS with image on Google's Messenger app. While some of the android device by default install this SMS app and for that when I send MMS using Intent than it's not working.

The problem is ToNumber and MMS content set but the image is not attach on this app.

Note: I already set the MMS APN setting on my devices,and i already check on multiple devices with same app like Samsung s4,Motorola G4 Plus.

This is my code currently I Used.

 String toNumbers = "comma seperated mobile numbers";

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) 
    {
        String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(getActivity()); 

        Intent sendIntent = new Intent(Intent.ACTION_SEND);
        sendIntent.putExtra("address", toNumbers);
        sendIntent.setPackage("com.android.mms");
        //Uri uri = Uri.fromFile(new File(getContext().getExternalFilesDir(""), "image.png"));

        File imagePath = new File(getFilesDir(), "images");
        File newFile = new File(imagePath, "image.png");
        Uri uri = getUriForFile(this, "packagename", newFile);

        File file = new File(contentUri.getPath());
        if (file.exists()) {
            //Do something
            Log.d("TAG","Exist");
        }
        sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
        sendIntent.setType("image/png");
        sendIntent.putExtra("sms_body", getString(R.string.sms_body, HostName));
        if (defaultSmsPackageName != null)
        {
            sendIntent.setPackage(defaultSmsPackageName);
        }
        startActivityForResult(sendIntent, Constants.SEND_SMS_REQUEST);


    }
    else 
    {
        Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW);
        smsIntent.putExtra("address", toNumbers);
        smsIntent.setPackage("com.android.mms");
        Uri uri = Uri.fromFile(new File(getContext().getExternalFilesDir(""), "image.png"));
        smsIntent.putExtra(Intent.EXTRA_STREAM, uri);
        smsIntent.setType("image/png");
        smsIntent.putExtra("sms_body", getString(R.string.sms_body, HostName));
        startActivityForResult(smsIntent, Constants.SEND_SMS_REQUEST);
    }

file_paths.xml

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path
    name="files"
    path="images/" />

</paths>

manifeast.xml

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="packagename"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />


    </provider>
Abhishek Patel
  • 4,280
  • 1
  • 24
  • 38
  • `getExternalFilesDir` returns a directory to which only your app has permissions. No other app can read from it. You'll need to expose your file via a [FileProvider](https://developer.android.com/reference/android/support/v4/content/FileProvider.html). – Eugen Pechanec Aug 29 '16 at 21:36
  • I already try with this,but its not working. – Abhishek Patel Sep 05 '16 at 04:42
  • **Well then update the question so we know what you're currently working with.** Are you absolutely sure a) you followed all the steps including granting temporary permissions b) the file path is right? External files dir points most likely to `/storage/emulated/0/Android/data//files` which is internal storage not SD card. – Eugen Pechanec Sep 05 '16 at 08:29
  • yes my files `Uri` is `content://mypackage/files/image.png` and the file is exist also on that path..,but when i programatically try to check exist the file it returns false..some strage issue produce. – Abhishek Patel Sep 05 '16 at 14:00
  • Post your file provider definition XML file. And body of getUriForFile method. – Eugen Pechanec Sep 05 '16 at 14:04
  • please show updated question – Abhishek Patel Sep 06 '16 at 04:34
  • `files-path` points to `/data/data//files` which is not externally accessible unless you have a rooted phone. This is not what you see when you connect the phone to your computer. You can use [CWAC-Provider](https://github.com/commonsguy/cwac-provider) which works very much like `FileProvider` except it supports `external-files-path`. – Eugen Pechanec Sep 06 '16 at 06:58
  • is the file you're sharing created by your app? – Pr38y Sep 22 '16 at 07:51

1 Answers1

1

file_paths.xml and manifest.xml are the same as in your code.

Create content uri:

File imagePath = new File(getFilesDir(), "images");
File newFile = new File(imagePath, "image.png");
Uri contentUri = FileProvider.getUriForFile(this, "packagename", newFile);

Check content uri:

ImageView imageView = (ImageView) findViewById(R.id.imageview);
//Your image should be displayed
imageView.setImageURI(contentUri);

Create intent:

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Text to send");
sendIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
sendIntent.setType("image/png");

Solution tested on:

a) Galaxy S4, Android 5.0, Messenger ver: 1.9.036

b) Emulator: Nexus 5, Android 6.0, Messaging ver: 1.0.001

wdeb
  • 61
  • 1
  • 4