Davinder Singh is a Polymath. He loves reading, learning and writing technical cookbooks about new technologies.
What You Will Learn in This Article?
You will learn how to use the camera module for taking pictures and videos.
You will learn how to connect and then program the camera module with Raspberry Pi.
You will learn how to use and implement the Twitter Api.
You will learn the internals of Android Things such as permissions, manifest and how to add external libraries in the project.
Lastly, you will learn how to handle Camera through Application Program Interface(API) framework provided by Android and thus you can take knowledge from here and make your own twitter client for Android Mobile Application.
What Will This Article Not Teach You?
This is surely not a “How to code in java” article. Hence, you won't learn Java in this one.
This is also not a “How to code?” article.
Before we start you will require following things by your side
Beginner or greater level of experience in programming.
Step 1: Download Twitter Java API
API or Application Program Interface is like a bridge between client (we) and service (in this case twitter). We will use twitter4j to access the twitter. Twitter4j is written in and for Java programming language hence the name. All android applications are written in Java or Kotlin (which in turn gets compiled to Java). Goto twitter4j's site and download latest version of library. It should be a zip file. There will be many directories inside the zip (Don't get panic!). We only require lib directory.
Step 2: Create a New Android Things Project
Let's create a new project. At this point I am assuming that you have already installed Android studio and Android software development kit(SDK) and that it's working. Start the studio and create a new project. If you are running studio version > 3.0 then goto Android Things tabs and select Android Things Empty Activity and click next. Otherwise, check Android Things checkbox right at the bottom of creating a new project dialog or window.
Step 3: Configure the Project
Step 4: Importing the Twitter4j
Before we can use twitter4j, We first have to import it into our project.
Goto lib directory in twitter4j's zip folder and copy all the files except twitter4j-examples-4.0.7.jar and Readme.txt.
Switch back to android studio and change project view type from android to project tree.
In the directory tree look for the lib directory and right click and then select paste and then OK. It will copy all the jar files in the lib folder.
Step 5: Adding Permissions in Manifest
Android operating system is very serious about security and hence it requires declaration of every hardware or features being used by application in application's manifest. Manifest is like a summary of android application. It contains features used by application, name of application, package name other metadata. We will be using Internet and Camera so application manifest must contain these two.
Goto Manifest file under manifest directory.
Add following lines after “<manifest .... >” tags.
At this point try building the project (Build -> Rebuild project). Make sure everything is error free.
Step 6: Adding a Camera Handler Class
In this step we will add a new class to the project containing all the code to manage the Camera for us.
Goto File and then New and click on create new java class
Give this class name CameraHandler
Note: In java name of class must match with the name of file otherwise java compiler will complain.
At this point your project should contain two files MainActivity and CameraHandler. We will alter MainActivity later. Let's add camera handling code in CameraHandler. I am assuming that you have at least beginner level experience in object oriented programming language that is not necessarily in Java.
Add following fields in the class. (As you typing these fields you will get error from IDE that following symbol is not found that's because the required library is not imported. Just hit ctrl + Enter or alt + Enter (Mac) and that should do the trick)
Now let's add a few constructors to the class and logic to initialise the camera. A constructor is a special function or method or block of code which contain the logic for creating the object out of class (A class is analogous to blueprint of building while an object is actual building)
After camera has been initialised, We need to add methods to control various other camera related tasks such as Image Capture, Saving Captured file and Shutting down the camera. These method uses code which is highly dependent on Android Framework and thus I'll not try to go in depth in it as this article is not about explaining the internals of framework. However you can see the android documentation here for further learning and research. For now just copy and paste the code.
Step 7: Take a Rest
Seriously, at this point you should take a moment to understand the code. Read the comment or have a sip of coffee. You have come a long way, and we are very close to our final thing.
Step 8: Creating a Twitter Application
Before we can access Twitter using twitter api we require some keys or secret passcodes which lets twitter's server know that we are legitimate developers and not here to abuse their's api. To get those passcodes we need to create an application in twitter's developer registry.
Create a new twitter developer request. Answer all the questions being asked by twitter and in the confirm your email address.
After confirming you will be forwarded to developer's dashboard. Click on create a new application.
Give app a name. In description write anything that you want (I wrote, “A bot that tweets images periodically.”) and lastly in website's url give name of website if you have otherwise type anything that qualifies as website url. And lastly at the end give 100 word description of application again use your creativity here. Once done click create app.
Step 9: The Twitter API
I am assuming you have correctly imported the twitter4j jars in lib directory inside the android things project. And project still builds fine without any errors (comment them if you have any I'll be happy to help). Now it's time to finally code the juicy part of application MainActivity (or whatever you named it).
Double click activity class to open it up in editor. Add the following fields inside the class.
Now let's complete the twitter part. Add following code inside your activity
Inside activity's onCreate method add the following code to get the twitter's instance and setup camera module.
You probably have errors at the moment. Let's solve them by adding more code or I should say missing code.
Step 10: Finalising the TwitterBot
And we are just few lines of code away from having our very own Twitter bot. We have Camera capturing images and twitter api we just have to bridge the both. Let's do this.
Connect the raspberry pi and camera module through the interface wires. Follow the instruction came with the camera module. Lastly connect raspberry pi with computer and run the project (green arrow over the top right side). Select your raspberry pi in the list. Wait for build and restarting. Camera module should start blinking and hopefully you will see some weird images on your twitter account wall. If you ran into problems, just comment and I'll help you out. Thank you for reading.