Facebook Integration In Android Tutorial

(Sending Images or Videos)

Facebook integration In Android Tutorial
Check out How To Integrate Facebook In Android (Part-I) before starting with this post. This Facebook Integration In Android Tutorial focuses on image and video integration process.
If you’re sharing images or video via the Facebook for Android app, you also need to declare theFacebookContentProvider in the manifest.
Append your app id to the end of the authorities value. For example if your Facebook app id is 1234, the declaration looks like:

<provider android:authorities=”com.facebook.app.FacebookContentProvider1234″
         android:name=”com.facebook.FacebookContentProvider”
         android:exported=”true” />

The Share button will call a Share dialog. To add a Share button in your onCreate()

ShareButton shareButton = new ShareButton(MainActivity.this);

People can share photos from your app to Facebook with the Share Dialog or with a custom interface.
The photos must be less than 12MB in size
People need the native Facebook for Android app installed, version 7.0 or higher
Build your share content for photos into the SharePhotoContent model. For a list of all attributes

Bitmap image = …
SharePhoto photo = new SharePhoto.Builder()
       .setBitmap(image)
       .build();
SharePhotoContent content = new SharePhotoContent.Builder()
       .addPhoto(photo)
       .build();

Videos

People using your app can share videos to Facebook with the Share dialog or with your own custom interface.
Videos must be less than 12MB in size.
Build your share content for videos into the ShareVideoContent model.

Uri videoFileUri = …
ShareVideo = new ShareVideo.Builder()
       .setLocalUrl(videoFileUri )
       .build();
ShareVideoContent content = new ShareVideoContent.Builder()
       .setVideo(video)
       .build();

Now we will check details about how to fetch photo and video from local device and share using above share dialog.
You can also start another activity and receive a photo/video result back using native button. To receive a result, call startActivityForResult() and call share dialog in onActivityResult().
Create two android native button for select photo and Video from device.

btnShareVideos.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
      Intent intent = new Intent(Intent.ACTION_PICK);
      intent.setType(“video/*”);
      startActivityForResult(intent, PICK_GALLERY_VIDEO);
  }
});

btnShareImages.setOnClickListener(new View.OnClickListener() {
  @Override
   public void onClick(View v) {
      Intent intent = new Intent(Intent.ACTION_PICK);
      intent.setType(“image/*”);
      startActivityForResult(intent, PICK_GALLERY_IMAGE);
  }
});

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  switch (requestCode) {
      case PICK_GALLERY_IMAGE:
          if (resultCode == Activity.RESULT_OK) {
               Uri uriPhoto = data.getData();
               Log.d(TAG, “Selected image path :” + uriPhoto.toString());

               Bitmap bitmap = null;
               try {
                     bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uriPhoto));
                    SharePhoto photo = new SharePhoto.Builder().setBitmap(bitmap)
                            .build();
                    SharePhotoContent content = new SharePhotoContent.Builder()
                            .addPhoto(photo).build();

                    shareButton.setShareContent(content);
                    shareButton.performClick();
               } catch (FileNotFoundException e) {
                    e.printStackTrace();
               } catch (Exception e) {
                     e.printStackTrace();
               }

               }
               break;
           case PICK_GALLERY_VIDEO:
                if (resultCode == RESULT_OK) {
                     Uri uriVideo = data.getData();

try {
                            String[] filePathColumn = {MediaStore.Images.Media.DATA};
                            Cursor cursor = getContentResolver().query(uriVideo,
                                                 filePathColumn, null, null, null);
                            if (cursor != null) {
                                 cursor.moveToFirst();
                                 int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                                 String videoPath = cursor.getString(columnIndex);
                                 cursor.close();
                                 Log.d(TAG, “Selected video path :” + videoPath);

ShareVideo video = new ShareVideo.Builder()
                                          .setLocalUrl(uriVideo).build();

ShareVideoContent content = new ShareVideoContent.Builder()
                                          .setVideo(video).build();

shareButton.setShareContent(content);
                                shareButton.performClick();
                  }
              } catch (Exception e) {
                  e.printStackTrace();
             }

          }
          break;
      default:
         callbackManager.onActivityResult(requestCode, resultCode, data);
      break;
  }
}

We hope this Facebook Integration in Android Tutorial will certainly help you.

Related Post

Leave a Comment