I'm trying to start an Intent from a Unity app that will launch the text messaging app with an attachment.
I have been able to open the text messaging app but the attachment does not load properly, throwing the following exception:
'Could not determine type of file:///storage/emulated/0/Android/data/com.torpedoesaway.memematch/files/Gifit2MemeFiles/gifit2meme-2019-09-7-09-39-54.gif java.io.IOException: java.lang.RuntimeException: setDataSource failed: status = 0x80000000'
Note that I have also tried loading other images, such as pngs and jpgs, all throwing the same error.
This is my code:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:" + recipient));
intent.putExtra("sms_body", message);
intent.putExtra(Intent.EXTRA_STREAM, attachment);
if (intent.resolveActivity(activity.getPackageManager()) != null) {
activity.startActivity(intent);
}
I tried playing around with the intent action as well as the setData
/setType
calls and in one instance I am able to open the chooser, select the messaging app and then the attachment is loaded properly. However, I want to open the text messaging app directly with the attachment working.
Thanks in advance for the help!
Edit:
How I'm making the call from Unity:
AndroidJavaClass Uri = new AndroidJavaClass("android.net.Uri");
AndroidJavaObject uri = Uri.CallStatic<AndroidJavaObject>("parse", path);
unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
pluginClass = new AndroidJavaObject("com.torpedosaway.giftomessage.Gif2Message");
pluginClass.Call(
"ComposeMmsMessage",
"53876045",
"message",
uri,
unityClass.GetStatic<AndroidJavaObject>("currentActivity"));
Just before you call startActivity(intent);
Add these lines:
StrictMode.VmPolicy.Builder builder = StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Then call
startActivity(intent);
Here is a possible answer to your question (Note the additional line in the code and the conversion of ACTION_SENDTO
to ACTION_SEND
) -
public void composeMmsMessage(String message, Uri attachment, long phoneNumber) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setData(Uri.parse("smsto:" + phoneNumber)); // This ensures only SMS apps respond
intent.putExtra("sms_body", message); // For the text that you want to send
intent.putExtra(Intent.EXTRA_STREAM, attachment); // For the atachment, which be a photo, video, file, etc. If you are using Firebase, you might want to take a look at the official Firebase Docs Page on this topic - https://firebase.google.com/docs/storage/unity/start
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
Here is the android mainfest -
<activity ...>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<data android:type="text/plain" />
<data android:type="image/*" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Hope this helps!!
Source - https://developer.android.com/
try this
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("sms_body", "Hi how are you");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File("/sdcard/file.gif")));
intent.setType("image/gif");
startActivity(Intent.createChooser(intent,"Send"));
User contributions licensed under CC BY-SA 3.0