Image sharing

Image capturing is no fun without the ability to share images; for example, on Facebook. We will be using the Facebook SDK for that.

Challenge! If you are building an app running on a Parse backend, as we did in Chapter 2, Applications with a Cloud-based Backend, there is no need for that, as the Facebook SDK is already in there. If you want, you can combine the recipes from Chapter 2, Applications with a Cloud-based Backend with this one, and create a real cool app real quick!

Getting ready

For this recipe, you need to have the previous recipe completed successfully and you need to have a real Android device (or a virtual one, but this will require some additional steps).

You also need to have a Facebook account, or you can create one just for testing purposes.

How to do it...

Let's take a look at how we can share our sepia captured image on Facebook:

  1. Get the code from the previous recipe. Open the build.gradle file in the app folder. Add a new dependency to the dependencies section, and click on the Sync now link that will appear after you have added this line:
    compile 'com.facebook.android:facebook-android-sdk:4.1.0'
    
  2. To obtain a Facebook app ID, browse to https://developers.facebook.com (yeah, this requires a Facebook account). From the MyApps menu, choose Add a new app, select Android as your platform, enter a name for your app, and click on Create new Facebook App ID. Choose a category- for example, Entertainment- and click on Create App ID.
  3. Your app will be created, and a QuickStart page will be shown. Scroll down all the way to the Tell us about your Android project section. Enter details in the package name and default activity class name fields, and click on the Next button.
  4. A pop-up warning will be shown. You can safely ignore the warning and click on the Use this package name button. Facebook will start thinking, and after a while the section Add your development and release key hashes will appear.
  5. To obtain development key hashes, open the Terminal app (in Windows, start Command Prompt) and type the following:
    keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64

    Tip

    If prompted for the keystore password, enter android, which should do the trick - unless you have changed the password previously, of course.

  6. Hit Enter, copy the value that is shown, and paste it into the Facebook web page at Development Key Hashes. Click on the Next button to proceed.
  7. In the section Next Steps, click on the Skip to developer dashboard button. It will bring you straight to the information you need, the app ID. Copy the value in the App ID field:
    How to do it...
  8. Next, initialize the Facebook SDK. Open the CameraActivity class, and within the onCreate method, add the following line just after the super.OnCreate line. Use the Alt + Enter shortcut to import the required package com.facebook.FacebookSdk:
    FacebookSdk.sdkInitialize(getApplicationContext());
  9. Now we need to tell the app about the Facebook app ID. Open the strings.xml file from the res/values folder. Add a new string that will contain your Facebook app id:
    <string name="facebook_app_id">Your facebook app id</string>
  10. Open the AndroidManifest.xml file.
  11. Add a metadata element to the application element:
    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
  12. Add a FacebookActivity declaration to the manifest file:
    <activity android:name="com.facebook.FacebookActivity"android:configChanges="keyboard|keyboardHidden|screenLayout|   
       screenSize|orientation"
      android:theme="@android:style/Theme.Translucent.
       NoTitleBar"
      android:label="@string/app_name" />
  13. In the Camera2BasicFragment class, locate the captureStillPicture method. Add a new call to the end of the onCaptureCompleted callback implementation, just after the unlockFocus class:
    sharePictureOnFacebook();
  14. Finally, add a provider to the manifest file (within the application section), which will allow you to share images on Facebook. The next chapter will discuss content providers. For now just append your app ID to the end of FaceBookContentProvider at authorities, replacing the zeros in the example shown here:
    <provider android:authorities="com.facebook.app. 
      FacebookContentProvider000000000000"android:name="com.facebook.FacebookContentProvider"android:exported="true" />
  15. Implement the sharePictureOnFacebook method. We will load the bitmap from the file. In a real app, we would have to calculate the required value for inSampleSize, but for the sake of simplicity, we will just use a fixed inSampleSize setting of 4 here. On most devices, this will be sufficient to avoid any OutOfMemory exceptions that may occur otherwise. Also, we will add the photo to the share dialog that will be displayed after taking a picture:
    private void sharePictureOnFacebook(){
        final BitmapFactory.Options options = new  
         BitmapFactory.Options();
        options.inJustDecodeBounds = false;
        options.inSampleSize = 4;
        Bitmap bitmap =  
         BitmapFactory.decodeFile(mFile.getPath(), options); 
        SharePhoto photo = new  
        SharePhoto.Builder().setBitmap(bitmap).build();
        SharePhotoContent content = new  
        SharePhotoContent.Builder().addPhoto(photo).build();
        ShareDialog.show(getActivity(), content);
    }
  16. To be on the safe side, we want to create a unique file name for each picture. Modify the onActivityCreated method to do so:
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mFile = new 
        File(getActivity().getExternalFilesDir(null),  
          "pic"+ new Date().getTime()+".jpg");
    }
  17. The page will look like this on your Facebook timeline. Here it is shown in the Dutch language:
    How to do it...
  18. Run the app and share some sepia images on your own Facebook timeline!

Our app is fully functional already, although it may require a few tweaks. On my Samsung device, all images that I have captured in portrait mode are rotated 90 degrees. That is just a little bit too artistic. Let's fix it in the next recipe!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset