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!
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.
Let's take a look at how we can share our sepia captured image on Facebook:
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'
keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64
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());
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>
AndroidManifest.xml
file.application
element:<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
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" />
Camera2BasicFragment
class, locate the captureStillPicture
method. Add a new call to the end of the onCaptureCompleted
callback implementation, just after the unlockFocus
class:sharePictureOnFacebook();
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" />
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); }
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"); }
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!