- Home /
Android Native share (intent) an image with a link
Hi, I'm trying to share via the android native sharing service a screenshot + a link to my application. The screenshot part is working perfectly though i don't see the text (tried to share with Facebook, twitter and whatsapp).
Here is my code (mostly taken from here):
public IEnumerator ShareScreenshot(string extraSubject, string extraText)
{
isProcessing = true;
// wait for graphics to render
yield return new WaitForEndOfFrame();
// create the texture
var width = Screen.width;
var height = Screen.height;
var tex = new Texture2D(width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
byte[] dataToSave = tex.EncodeToPNG();
string destination = Path.Combine(Application.persistentDataPath, System.DateTime.Now.ToString("yyyy-MM-dd-HHmmss") + ".png");
File.WriteAllBytes(destination, dataToSave);
if (!Application.isEditor)
{
// block to open the file and share it ------------START
AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse", "file://" + destination);
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObject);
intentObject.Call<AndroidJavaObject>("setType", "text/plain");
//add data to be passed to the other activity i.e., the data to be sent
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_SUBJECT"), extraSubject);
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), extraText);
intentObject.Call<AndroidJavaObject>("setType", "image/jpeg");
AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jChooser = intentClass.CallStatic<AndroidJavaObject>("createChooser", intentObject, "Share Via");
AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
currentActivity.Call("startActivity", jChooser);
}
isProcessing = false;
buttonShare.enabled = true;
ButtonRestart.enabled = true;
}
Facebook totally restricted this kind of sharing. You need to sign in into facebook account to perform this kind of sharing. I don't know about Twitter and Whatsapp. $$anonymous$$ay be they also follow same strategy now. Some time ago for Twitter working for me, text and image sharing.
Answer by hybridAli · May 01, 2017 at 09:41 AM
I have a blog post about what you need http://alihc.me/unity-3d-native-nativeshare/
This link is broken. Please remove it or update it. Thanks
Answer by Shasha1110 · Oct 10, 2017 at 11:40 AM
Hi , I have followed this code for snapshot sharing. It works fine except 7.0 devices. The intent chooser option window doesn't open in 7.0 and above devices. Can anybody guide me how to resolve this? Thanks in advance .
Hy, i have the same problem, did you find any soluton?
Thanks!
Answer by SmileSoft4849 · Mar 18, 2018 at 04:49 AM
I have Created a Unity Android share plugins which can share both Images and video files and support almost all Android versions. You can Check out it from the asset store.
Link : https://assetstore.unity.com/packages/tools/integration/android-native-share-127249
@SmileSoft4849, You must help users by providing appropriate answers, ins$$anonymous$$d you are marketing your useless products so that you get money while we purchase your asset. This is users community, not your market place. Please bear that in $$anonymous$$d.
For those having issue with Native Sharing get this code. It helped me a lot Get source code from here https://gist.github.com/agrawalsuneet/a39cdbee9fa093a16b817742b602f9e7
For Reference Go to https://medium.com/@agrawalsuneet/native-android-screenshot-image-sharing-in-unity-47eda326b80c
I do not know if it works in Android Oreo or later, mention if you find it working in Android Oreo or later.
Answer by junedmmn · Apr 19, 2019 at 06:47 PM
For those who didn't find the answer try this code. It worked for me.
Get Source Code from https://gist.github.com/agrawalsuneet/a39cdbee9fa093a16b817742b602f9e7
For reference go to https://medium.com/@agrawalsuneet/native-android-screenshot-image-sharing-in-unity-47eda326b80c
Please mention, if it works in Android Oreo or later.
Your answer
Follow this Question
Related Questions
NoSuchMethodError when calling putExtra 0 Answers
Facebook app review for screenshot share 0 Answers
Share screen and text on social media 1 Answer
Open Android App from URL 3 Answers
Share image in Unity Game on Android devices error 2 Answers