Working with Images in Google's Android
Google's Android platform really took the developers' world by storm with the announcement of the 10 million dollar challenges. Since its inception in late 2007, almost 1,800 new software entries had been submitted from all over the world and some of them already made the technical news headlines because of their innovative ideas. A few interesting concepts have been introduced in this platform although, at the beginning, some developers thought it could be nothing but a combination of Linux and Java along with Google's own APIs. By way of focusing on image-related work, this article should give you an idea how easy it is to work with Android and how powerful this platform is.
What Do You Need to Start?
At the moment, there are very few references available about Android. The best source is from Google's own Android site at http://code.google.com/android/. It has all the necessary development tools, plug-ins, and sample codes you need to download. Simply follow the step-by-step instructions to get them all. It surely does not make much sense for me to repeat here. In addition to the online documentation, you can look for technical help by participating in the community forums. If you are a complete novice about Android, I would recommend you choose the free development tool Eclipse because it integrates better with Android SDK as well as the debugging software and emulators. There are other ways of building projects through command-line or batch scripts, but using Eclipse should be the easiest way to start, according to many fellow developers' experience.
What APIs Are Available for Processing Images?
Android's APIs cover lots of great features, including:
- SQLite for structured data storage: You can embed a tiny database with your application without much effort.
- Graphics library support: Optimized 2D graphics library and 3D graphics based on the embedded version of OpenGL ES.
- Integrated web browser support
- Media support: This supports common audio, video, and still-image formats.
- Google APIs: Mapping capability allows third-party code to display and control a Google Map. It also supports P2P services via XMPP called GTalkService.
- Hardware-dependent support: There are many expected APIs for GSM Telephony, Bluetooth, EDGE, 3G, WiFi, camera, Location-Based Services (via GPS and so forth), compass, and accelerometer.
Out of the large collection of APIs, you'll mainly look at those under these two packages extracted from the online description:
- android.graphics: This provides low-level graphics tools such as canvases, color filters, points, and rectangles that let you handle drawing to the screen directly.
- android.graphics.drawable: This provides classes to manage a variety of visual elements that are intended for display only, such as bitmaps and gradients.
Images are bitmaps, so you'll rely heavily on the APIs under android.graphics.Bitmap.
File I/O and Supported Image Formats
Android supports several common still-image formats such as PNG, JPG, and GIF. In the example, you will use the JPG format. If you consider using the transparency feature for the image, the PNG format would be a better choice.
To read an image file that is packaged with your software, you should put it under the res/drawable folder relative to your software's root. Once the image is in the folder, a resource ID will be generated automatically when you re-compile the package. For example, you have the image file called pic1.jpg. It will become accessible programmatically through its resource ID R.drawable.pic1. You can see that the image file extension has been stripped off and the upper case R represents the overall resource file, R.java. R.java is generated automatically and should not be edited unless you have a pretty good understanding of how resources are structured in this file. Here is the code segment showing how you can refer to an image through its resource ID.
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.pic1);
int pic_width = mBitmap.width();
int pic_height = mBitmap.height();
To read or write an image file without specifying the folder structure, it will be under /data/data/YourPackageName/files/ on the emulator. For example, you will create your package name for your example as package com.cyl.TutorialOnImages. Therefore, if you create a new image file at runtime, it will be under the /data/data/com.cyl.TutorialOnImages/files/ folder. Please note that each Android application will start with its own user and group ID, so some file folders are not accessible through your software unless they are specifically set to do so. Here is the code for when you want to output a bitmap onto a file called output.jpg.
try {
FileOutputStream fos = super.openFileOutput("output.jpg",
MODE_WORLD_READABLE);
mBitmap.compress(CompressFormat.JPEG, 75, fos);
fos.flush();
fos.close();
} catch (Exception e) {
Log.e("MyLog", e.toString());
}
Image View, Colors, and Transparency
Each Android application should have a screen layout. It can be created dynamically inside the software, or it can be specified through an external XML file. The default is main.xml. To contain an image, you use a View class called ImageView. Here is what the content of the main.xml file looks like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/
apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView id="@+id/picview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>