December 21, 2014
Hot Topics:

Working with Images in Your Android App Development

  • Updated: January 9, 2012
  • By Chunyen Liu
  • Send Email »
  • More Articles »

The Android platform continues to enjoy increasing popularity among software developers. It started in late 2007 with two multi-million-dollar design contests that attracted thousands of talented developers from all over the world, and several of them made tech news headlines because of their innovative ideas. Many interesting concepts have been introduced in this platform since, but at the beginning some developers thought it would be nothing but a combination of Linux and Java along with Google APIs.

While focusing on image-related work on Android, this article should give you an idea of how easy it is to work with Android and how powerful this platform is. Click here to download the source files for the working sample project I'll refer to in this tutorial.

 


Visit the Android Dev Center

 

What Do You Need to Start?

At the moment, the best resource for Android developers is Google's own developer.android.com/ site. It has all the necessary development tools, plug-ins, and sample code you need. Simply follow the step-by-step instructions for installation. It does not make much sense for me to repeat here. The site also contains information about how to register and publish your application on Google's Android Market.

In addition to the online documentation, you can look for technical help by participating in the Android community forums. If you are a complete novice, use the free development tool Eclipse because it integrates better with the Android SDK as well as providing 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.

Android APIs 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: You can easily integrate a Web view inside your software.
  • Media support: This supports common audio, video, and still-image formats, as well as text-to-speech (TTS) and voice recognition with a very straightforward interface.
  • Google APIs: Mapping capability allows third-party code to display and control a Google Map.
  • Hardware-dependent support: There are many APIs for NFC, telephony, Bluetooth, 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 the following two packages (as explained in the online description):

  • android.graphics: This package 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 package 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.

Android 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. After the image is in the folder, a resource ID will be generated automatically when you recompile 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 a code snippet showing how you can refer to an image through its resource ID.

Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic1);
int mPhotoWidth = mBitmap.getWidth();
int mPhotoHeight = mBitmap.getHeight();

To read or write an image file without specifying the folder structure, find it under /data/data/YourPackageName/files/ on the emulator. For example, you will create the package name for the example in this tutorial as package cliu.TutorialOnImages. Therefore, if you create a new image file at runtime, it will be under the /data/data/cliu.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 with a quality measure of 75% of the original image.

In our example, the result is saved onto an SD card. Android requires special permission for this purpose. In the manifest file, you will need to add the following line:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Here is how we make sure our folder exists:

String dirname = Environment.getExternalStorageDirectory() + "/tutorialonimages/";
File sddir = new File(dirname);
if (!sddir.mkdirs()) {
    if (sddir.exists()) {
    } else {
        Toast.makeText(TutorialOnImages.this, "Folder error", Toast.LENGTH_SHORT).show();
        return;
    }
}

Finally, we can save the resulting image onto the SD card under our folder.

try {
    FileOutputStream fos = new FileOutputStream(dirname + "output.jpg");
    mBitmap.compress(CompressFormat.JPEG, 75, fos);

    fos.flush();
    fos.close();
} catch (Exception e) {
    Log.e("MyLog", e.toString());
}

Image View, Colors, and Transparency in Android

Each Android application should have a screen layout. It can be created dynamically inside the software (used in our implementation), or it can be specified through an external XML file as in the example below. 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>

Just as the image file can be accessed through a resource ID, main.xml will have a resource ID called R.layout.main automatically generated in the global resource file R.java after compilation. Figure 1 shows the initial view when the application first starts on the emulator.

Android Image Processing

Figure 1: Software First Starts On The Emulator

Each image pixel is represented as a 4-byte integer. The highest byte is for the alpha channel; in other words, the opacity/transparency control. A value of 255 means the pixel is completely opaque; 0 means it is entirely transparent. The next byte is for the red channel; 255 represents a fully saturated red. Similarly, the next two bytes are for the green and blue channels, respectively.


Tags: Android, Android App development



Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel