2. ABOUT: ME
‣ Android engineer, Bump Technologies
‣ ex-eBay: eBay Mobile for Android
‣ Android in Practice
‣ Social info
‣ @michaelg
‣ +Michael Galpin
4. You down with AIP?
• Chapter 11: “Appeal to
the senses using
multimedia.”
• Slideshow app
MediaMogul.apk
• http://
code.google.com/p/
android-in-practice/
15. Check the front
private boolean hasFrontFacingCamera(){
PackageManager mgr = this.getPackageManager();
for (FeatureInfo fi : mgr.getSystemAvailableFeatures()){
if (fi.name.equals(PackageManager.FEATURE_CAMERA_FRONT)){
return true;
}
}
return false;
}
private Camera getFrontFacingCamera(){
for (int i=0;i<Camera.getNumberOfCameras();i++){
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT){
return Camera.open(i);
}
}
return null;
}
16. Check the front
private boolean hasFrontFacingCamera(){
PackageManager mgr = this.getPackageManager();
for (FeatureInfo fi : mgr.getSystemAvailableFeatures()){
if (fi.name.equals(PackageManager.FEATURE_CAMERA_FRONT)){
return true;
}
}
return false;
}
private Camera getFrontFacingCamera(){
for (int i=0;i<Camera.getNumberOfCameras();i++){
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT){
return Camera.open(i);
}
}
return null;
}
34. Just give it to me /res/raw
Resources res = this.getResources();
InputStream stream = res.openRawResource(R.raw.sunflower);
MediaPlayer.create(this, R.raw.constancy).start();
43. Loading local images
InputStream stream = new FileInputStream(imgFile);
Bitmap bm = BitmapFactory.decodeStream(stream);
// calculate desired width and height
Bitmap thumb = ThumbnailUtils.extractThumbnail(bm, width, height);
Bitmap thumb = Bitmap.createScaledBitmap(bm, width, height, false);
44.
45. Loading without
exploding
public static Bitmap decodeDownsizedBitmapStream(File file, int target, Context context) throws
IOException {
FileInputStream stream = new FileInputStream(file);
Pair<Integer, Integer> source = getDimensionsForStream(stream);
stream.close();
FileInputStream in = new FileInputStream(file);
Options options = new Options();
options.inSampleSize = 1 + getDownSampleSize(max(source.first, source.second), target);
return BitmapFactory.decodeStream(in, null, options);
}
public static Pair<Integer, Integer> getDimensionsForStream(InputStream in){
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, options);
return new Pair<Integer, Integer>(options.outWidth, options.outHeight);
}
public static int getDownSampleSize(int source, int target){
int size = 1;
if (source <= 2*target){
int power = (int) ((log (source / target)) / log(2));
size = (int) pow(2, power);
}
return size;
}
46. Loading web images
URL url = new URL(urlString);
// NOTE, be careful about just doing "url.openStream()"
// it's a shortcut for openConnection().getInputStream() and doesn't set timeouts
// the defaults are "infinite" so it will wait forever if endpoint server is down
// do it properly with a few more lines of code . . .
URLConnection conn = url.openConnection();
conn.setConnectTimeout(3000);
conn.setReadTimeout(5000);
Bitmap bitmap = BitmapFactory.decodeStream(conn.getInputStream());
56. Sidebar: Bitmaps & Heap
Reported by andreas....@googlemail.com, May 22, 2010
Note: This is NOT a requst for assistance!
In my applications, I keep getting the following exception:
E/AndroidRuntime( 1420): java.lang.OutOfMemoryError: bitmap size exceeds
VM budget
E/AndroidRuntime( 1420):
at
android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
E/AndroidRuntime( 1420):
at
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459)
E/AndroidRuntime( 1420):
at
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:515)
E/AndroidRuntime( 1420):
at
de.schildbach.bitmapbug.MyActivity.bitmap(MyActivity.java:38)
Comment 1 by romaingu...@gtempaccount.com, May 23, 2010
Your app needs to use less memory.
http://code.google.com/p/android/issues/detail?id=8488
57.
58. Use Placeholders
ImageView image =
(ImageView) convertView.findViewById(R.id.deal_img);
image.setImageBitmap(
BitmapFactory.decodeResource(
getResources(), R.drawable.temp));
Item item = getItem(position);
new RetrieveImageTask(image).
execute(item.getSmallPicUrl());
59. @Override
View Holders
public View getView(final int position, View cell, ViewGroup parent) {
ViewHolder holder = (ViewHolder) cell.getTag();
if (holder == null){
holder = new ViewHolder(cell);
cell.setTag(holder);
}
Bitmap thumb = (Bitmap) getItem(position);
holder.img.setImageBitmap(thumb);
File file = getImageFile(position);
if (selectedFiles.contains(file)){
holder.cbox.setChecked(true);
} else {
holder.cbox.setChecked(false);
}
return cell;
}
static class ViewHolder {
final ImageView img;
final CheckBox cbox;
ViewHolder(View cell){
img = (ImageView) cell.findViewById(R.id.thumb);
cbox = (CheckBox) cell.findViewById(R.id.cbox);
}
}