Images local to an application are usually put in the res/drawable/ directory, as discussed in Chapter 4,"User Interface Layout," and are packaged with the application. They can be accessed with the appropriate resource identifier, such as R.drawable.my_picture. Images on the Android device filesystem can be accessed using the normal Java classes, such as an InputStream. However, the preferred method in Android to read an image into memory for manipulation is to use the built-in class BitmapFactory.

BitmapFactory creates Bitmap objects from files, streams, or byte-arrays. For the two previous examples:

Bitmap myBitmapl = BitmapFactory.decodeResource(getResources(),

R.drawable.my_picture); Bitmap myBitmap2 = BitmapFactory.decodeFile(filePath);

After the image is in memory, it can be manipulated using the bitmap methods, such as getPixel() and setPixel(). However, most images are too large to manipulate full scale on an embedded device. Instead, consider subsampling the image:

Bitmap bm = Bitmap.createScaledBitmap(myBitmap2, 480, 320, false); This avoids OutOfMemory run-time errors.

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook

Post a comment