Unity Share Image to Facebook - android

I would like to share image to Facebook for Android platform without using Native share sheet (chooser) and my intent is
click the custom designed button.
move to facebook app
open facebook share page or post directly.
but I can't find any method neither in the documentation or in the any website.
and I don't want to set up Fb SDK because of the version compatibility in the future.
It would be so thankful with Unity JNI way or Native Android way to implement this which meets the condition.
I tried as following methods.
facebook native android sharing
JNI (but it's implementation of using Chooser).
using AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
using AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent");
using AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
using AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
using AndroidJavaObject unityContext = currentActivity.Call<AndroidJavaObject>("getApplicationContext");
intent.Call<AndroidJavaObject>("setPackage", "com.facebook.katana");
intent.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
intent.Call<AndroidJavaObject>("setType", "image/*");
using var file = new AndroidJavaObject("java.io.File", path);
using var fileProvider = new AndroidJavaClass("androidx.core.content.FileProvider");
string packageName = unityContext.Call<string>("getPackageName");
string authority = packageName + ".fileprovider";
using var uri = fileProvider.CallStatic<AndroidJavaObject>("getUriForFile", unityContext, authority, file);
intent.Call<AndroidJavaObject>("addFlags", intentClass.GetStatic<int>("FLAG_GRANT_READ_URI_PERMISSION"));
intent.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uri);
using var chooser = intentClass.CallStatic<AndroidJavaObject>("createChooser", intent, "Share Image to Facebook");
currentActivity.Call("startActivity", chooser);

Related

I want to make android application to send picture, message to instagram users using instagram API

I've tried several sources from Google and GitHub but didn't find any authentic source that could help me sending multiple pictures and posts automatically from my android gallery using the scheduler. Is anyone working with the Instagram API? If so, could you please give me some authentic source?
Instead of using the Instagram API, you can directly start an Intent to post an Image or Video to Instagram by opening a "Share with" dialog. This will require user interaction.
String type = "image/*";
String filename = "/myPhoto.jpg";
String mediaPath = Environment.getExternalStorageDirectory() + filename;
private void createInstagramIntent(String type, String mediaPath){
// Create the new Intent using the 'Send' action.
Intent share = new Intent(Intent.ACTION_SEND);
// Set the MIME type
share.setType(type);
// Create the URI from the media
File media = new File(mediaPath);
Uri uri = Uri.fromFile(media);
// Add the URI to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);
// Broadcast the Intent.
startActivity(Intent.createChooser(share, "Share to"));
}
Code example taken from https://www.instagram.com/developer/mobile-sharing/android-intents/

how to share images inside assets folder with intent to other application

i want to share images inside an assets folder using intent to the following applications
hangout
whatsapp
line chat
viber
tango
wechat
i have try this code for whatsapp but it given me file not supported
public void share (){
String file = "file:///android_asset/food/apple.png";
Uri filePath = Uri.fromFile(new File("content://com.example.zainabishaqmusa.postemoji/assets/gestures/aok.png"));
final ComponentName name = new ComponentName("com.whatsapp", "com.whatsapp.ContactPicker");
Intent oShareIntent = new Intent();
oShareIntent.setComponent(name);
//oShareIntent.setType("text/plain");
//oShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Website : www.google.com");
oShareIntent.putExtra(Intent.EXTRA_STREAM, filePath);
oShareIntent.setType("image/png");
oShareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Gesture.this.startActivity(oShareIntent);
}
You would need a ContentProvider that is capable of sharing content from assets. My StreamProvider offers this, or you could write your own.

Share Button to social Media Unity (Android)

I am trying to create a share button in Unity. Just one button that when is pressed it will show the social media applications that is installed on your phone, and allows the user to share. I keep finding tutorials over how to create a facebook share button or a twitter share button. But I just want to create a simple share button that allows you to share with every social media application. Here is an example:
EXAMPLE
I found a few assets, but not for sure if they will work right.
Assets: https://www.assetstore.unity3d.com/en/#!/content/37320
This asset allows you to share an image, I don't need to share an image, just text. But I thought it wouldn't be hard to modify it and only do text.
There's two ways of doing this.
1. Create a native plugin, write wrapper code in Unity to call the native code (Probably the most widely used way to call native functions)
2. Write the code entirely in Unity, and use AndroidJavaObject to invoke the functions.
Option 1 - Native Java Code + Unity Wrapper
Here's a link I found on SO for the code for Sharing.
Here's a link to one of my older answers about plugins. You can modify the code there to fit your needs.
Option 2 - No native code.
This way is a little more interesting. We use Unity's AndroidJavaClass & AndroidJavaObject to eliminate the need of JARs altogether. Just stick the below code in a C# script, and call the function. (NOTE, I haven't tried this code, there may be errors. If there are, let me know and I'll edit my response)
private static AndroidJavaObject activity = null;
private static void CreateActivity () {
#if UNITY_ANDROID && !UNITY_EDITOR
if(activity == null)
activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").
GetStatic<AndroidJavaObject>("currentActivity");
#endif
}
public static void ShareActivity (string title, string subject, string body) {
CreateActivity();
AndroidJavaObject sharingIntent = new AndroidJavaObject("android.content.Intent", "android.intent.action.SEND")
.Call<AndroidJavaObject>("setType", "text/plain")
.Call<AndroidJavaObject>("putExtra", "android.intent.extra.TEXT", body)
.Call<AndroidJavaObject>("putExtra", "android.intent.extra.SUBJECT", subject);
AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", activity)
.CallStatic<AndroidJavaObject>("createChooser", sharingIntent, title);
activity.Call("startActivity", intent);
}
Don't forget to add the activity to your AndroidManifest.xml!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.InteropServices;
public class socioshare : MonoBehaviour
{
string subject = "Hey I am playing this awesome new game called SoccerCuby,do give it try and enjoy \n";
string body = "https://play.google.com/store/apps/details?id=com.KaliAJStudios.SoccerCuby";
public void OnAndroidTextSharingClick()
{
//FindObjectOfType<AudioManager>().Play("Enter");
StartCoroutine(ShareAndroidText());
}
IEnumerator ShareAndroidText()
{
yield return new WaitForEndOfFrame();
//execute the below lines if being run on a Android device
//Reference of AndroidJavaClass class for intent
AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
//Reference of AndroidJavaObject class for intent
AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
//call setAction method of the Intent object created
intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
//set the type of sharing that is happening
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"), subject);
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TITLE"), "TITLE");
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), subject);
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), body);
//get the current activity
AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
//start the activity by sending the intent data
AndroidJavaObject jChooser = intentClass.CallStatic<AndroidJavaObject>("createChooser", intentObject, "Share Via");
currentActivity.Call("startActivity",jChooser);
}
}
unable to display the 1st string i.e "subject" in the message. the 2nd string "body" is being displayed accurately.rest everything is working Fine.

Android share intent for Pinterest not working

I am doing an android share intent for Pinterest but is not fully working. I am able to attach the image but I can't send text to the "description" field in the share window. I've tried different types (text/plain, image/*, image/png) and also tried the ACTION_SEND_MULTIPLE intent type but still no luck. Google chrome share intent works perfectly so I'm sure Pinterest supports this functionality. Here is my code:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_TEXT, text);
if(file != null) intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setClassName(packageName, name);
this.startActivity(intent);
Any idea? thanks!
I found a way to share to Pinterest with plain Android intents (without using the Pinterest SDK), with help from Pin It button developer docs.
Basically you just open an URL like this with Intent.ACTION_VIEW; the official Pinterest app kindly supports these URLs. (I've earlier used a very similar approach for sharing to Twitter.)
https://www.pinterest.com/pin/create/button/
?url=http%3A%2F%2Fwww.flickr.com%2Fphotos%2Fkentbrew%2F6851755809%2F
&media=http%3A%2F%2Ffarm8.staticflickr.com%2F7027%2F6851755809_df5b2051c9_z.jpg
&description=Next%20stop%3A%20Pinterest
And for smoother user experience, set the intent to open directly in Pinterest app, if installed.
A complete example:
String shareUrl = "https://stackoverflow.com/questions/27388056/";
String mediaUrl = "http://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png";
String description = "Pinterest sharing using Android intents"
String url = String.format(
"https://www.pinterest.com/pin/create/button/?url=%s&media=%s&description=%s",
urlEncode(shareUrl), urlEncode(mediaUrl), urlEncode(description));
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
filterByPackageName(context, intent, "com.pinterest");
context.startActivity(intent);
Util methods used above:
public static void filterByPackageName(Context context, Intent intent, String prefix) {
List<ResolveInfo> matches = context.getPackageManager().queryIntentActivities(intent, 0);
for (ResolveInfo info : matches) {
if (info.activityInfo.packageName.toLowerCase().startsWith(prefix)) {
intent.setPackage(info.activityInfo.packageName);
return;
}
}
}
public static String urlEncode(String s) {
try {
return URLEncoder.encode(s, "UTF-8");
}
catch (UnsupportedEncodingException e) {
Log.wtf("", "UTF-8 should always be supported", e);
return "";
}
}
This is the result on a Nexus 5 with Pinterest app installed:
And if Pinterest app is not installed, sharing works just fine via a browser too:
for some reason pinterest app doesn't comply to the standard (Intent.EXTRA_TEXT) so we have to add it separately
if(appInfo.activityInfo.packageName.contains("com.pinterest"){
shareIntent.putExtra("com.pinterest.EXTRA_DESCRIPTION","your description");
}
File imageFileToShare = new File(orgimagefilePath);
Uri uri = Uri.fromFile(imageFileToShare);
Intent sharePintrestIntent = new Intent(Intent.ACTION_SEND);
sharePintrestIntent.setPackage("com.pinterest");
sharePintrestIntent.putExtra("com.pinterest.EXTRA_DESCRIPTION", text);
sharePintrestIntent.putExtra(Intent.EXTRA_STREAM, uri);
sharePintrestIntent.setType("image/*");
startActivityForResult(sharePintrestIntent, PINTEREST);

Facebook Link doesnt work android

Hello I just want to call Facebook app with the link below (on my android project):
String url_facebook_prixo = "https://www.facebook.com/pages/Prixo/468580313168290";
I tried this :
Uri uri = Uri.parse("facebook://facebook.com/page{468580313168290}");
Intent viewIntent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(viewIntent);
I tried with others link but its only displaying my wall..
Someone?
You should use the id to open the page.
Intent facebookIntent = new Intent(
Intent.ACTION_VIEW,
Uri.parse("fb://profile/468580313168290"));
startActivity(facebookIntent);
I would advice you to encapsulate this in a try catch. Inside the catch open a normal browser intent

Categories

Resources