Android: Share with Facebook
Facebook doesn't work well with normal sharing intents when sharing multiple content elements as discussed in this bug. To share posts with facebook, we need to:
- Create a new Facebook app here (follow the instructions)
- Add the Facebook SDK to your Android project:
SetUp:
a. App/build.gradle (add below)
repositories { mavenCentral() } dependencies {
compile 'com.facebook.android:facebook-android-sdk:4.7.0' }
b. Add you Facebook app id to strings.xml
<string name="facebook_app_id">1698838388383</string>
c. Add Facebook activity to AndroidManifest.xml, also add <meta-data AppID />
Activity:
<!-- Share to Facebook -->
<activity android:name="com.facebook.FacebookActivity"></activity>
Meta-data:
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />
b. Add you Facebook Library to your AndroidStudio project.
1. Download SDK from here: Facebook SDK
2. unzip
3. On Android Studio go to:
- File -> New -> New Module
- Select "Import .JAR/.AAR package"
- Go to your unzip Facebook SDK, and select facebook-android-sdk-4.7.0.aar
- Click next. Done :)
Share from Menu bar:
ShareFB.java , menu_shareFB.xml
a. edit menu_shareFB.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/action_share" android:icon="@android:drawable/ic_menu_share" android:title="Some title" app:showAsAction="always" > <menu> <item android:id="@+id/action_share_Facebook" android:title="Share with Facebook"/> <item android:id="@+id/action_share_other_Apps" android:title="Share with other Apps"/> </menu> </item></menu>
b. Now edit ShareFB.java:
TODO:
add github repo here:
Reference:@Overridepublic boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_campaign_detail, menu); return true;} @Overridepublic boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()) { case R.id.action_share_Facebook: { setupFacebookShareIntent(); Toast.makeText(getApplicationContext(), "here facebok", Toast.LENGTH_LONG).show(); } break; // case R.id.action_share: case R.id.action_share_other_Apps: { setupShareIntent(); Toast.makeText(getApplicationContext(), "here 2", Toast.LENGTH_LONG).show(); } break; } return super.onOptionsItemSelected(item);} //Share with Facebookpublic void setupFacebookShareIntent() { ShareDialog shareDialog; FacebookSdk.sdkInitialize(getApplicationContext()); shareDialog = new ShareDialog(this); ShareLinkContent linkContent = new ShareLinkContent.Builder() .setContentTitle("SumOfUs") .setContentDescription( "\"Title Of Test Post\"") .setContentUrl(Uri.parse("http://www.sumofus.org")) .build(); shareDialog.show(linkContent); //MessageDialog.show(this, linkContent);} // Gets the image URI and setup the associated share intent to hook into the provider public void setupShareIntent() { // Fetch Bitmap Uri locally ImageView ivImage = (ImageView)findViewById(R.id.ivCampaign); // Get access to the URI for the bitmap Uri bmpUri = getLocalBitmapUri(ivImage); if (bmpUri != null) { // Construct a ShareIntent with link to image Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri); shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Title Of Test Post"); shareIntent.putExtra(Intent.EXTRA_TEXT, "https://www.sumofus.org"); shareIntent.setType("image/*"); // Launch sharing dialog for image startActivity(Intent.createChooser(shareIntent, "send")); } else { Toast.makeText(this, "Some error occured during sharing", Toast.LENGTH_LONG).show(); } } // Returns the URI path to the Bitmap displayed in specified ImageView public Uri getLocalBitmapUri(ImageView imageView) { // Extract Bitmap from ImageView drawable Drawable drawable = imageView.getDrawable(); Bitmap bmp = null; if (drawable instanceof BitmapDrawable){ bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); } else { return null; } // Store image to default external storage directory Uri bmpUri = null; try { File file = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png"); file.getParentFile().mkdirs(); FileOutputStream out = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.PNG, 90, out); out.close(); bmpUri = Uri.fromFile(file); } catch (IOException e) { e.printStackTrace(); } return bmpUri; }
TODO:
add github repo here:
Share using this code snippet:
public void setupFacebookShareIntent() {
ShareDialog shareDialog;
FacebookSdk.sdkInitialize(getApplicationContext());
shareDialog = new ShareDialog(this);
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentTitle("Title")
.setContentDescription(
"\"Body Of Test Post\"")
.setContentUrl(Uri.parse("http://someurl.com/here"))
.build();
shareDialog.show(linkContent);
}
http://guides.codepath.com/android/Sharing-Content-with-Intents#share-in-facebook
No comments:
Post a Comment