Android

Show image using ImageView

 * Suppose you have a resource file named "res/drawable-xhdpi/ic_action_search.png"


 * Add following to res/layout/xxx.xml

ImageView imageView = (ImageView) findViewById( R.id.imageView1 ); imageView.setImageResource( R.drawable.ic_action_search );
 * In the XXXActivity.java, add following

BitmapFactory.Options options = new BitmapFactory.Options;
 * To load and show a smaller version of the image

// to load a smaller image options.inSampleSize = 2; options.inJustDecodeBounds = false;

Bitmap image_bit_map = BitmapFactory.decodeResource( getResources, R.drawable.ic_action_search, options );

ImageView imageView = (ImageView) findViewById( R.id.imageView1 ); imageView.setImageBitmap( image_bit_map );

Loading image properties

 * Suppose you have a resource file named "res/drawable-xhdpi/ic_action_search.png"

BitmapFactory.Options options = new BitmapFactory.Options;

// // http://developer.android.com/training/displaying-bitmaps/load-bitmap.html // // Setting the inJustDecodeBounds property to true while decoding avoids memory // allocation, returning null for the bitmap object but setting outWidth, // outHeight and outMimeType. This technique allows you to read the dimensions // and type of the image data prior to construction (and memory allocation) of // the bitmap. // options.inJustDecodeBounds = true;

BitmapFactory.decodeResource( getResources, R.drawable.ic_action_search, options );

int imageHeight = options.outHeight; int imageWidth  = options.outWidth; String imageType = options.outMimeType;

System.out.println( "w: " + imageWidth + " h: " + imageHeight + " type: " + imageType );