Click here to Skip to main content
13,048,406 members (141,540 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


7 bookmarked
Posted 25 Jul 2013

Android - Image Operations Using BitmapFactory

, 30 Jul 2013
Rate this:
Please Sign up or sign in to vote.
Walltoday is an app for setting a wallpaper for each day.


Though it may seem easy to set an image as a wallpaper, there are various small issues that need to be addressed to correctly set a wallpaper. Through a simple app - Walltoday which allows a user to set a wallpaper for each day which changes automatically every morning, this tip shows a number of points which will help in setting the wallpaper.


The app can be downloaded via Google play store using the following links:

  • market://details?


Using the Code

1. Activity

Start activity to select an image from Image Gallery:

Intent intent = new Intent(Intent.ACTION_PICK,
startActivityForResult(intent, RESULT_LOAD_IMAGE);

Set the selected image from gallery:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
    if (requestCode == RESULT_LOAD_IMAGE && 
            resultCode == RESULT_OK && null != data) 
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(
                              selectedImage,filePathColumn, null, null, null);
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        ImageButton imgButton = (ImageButton) findViewById(ButtonIdToSet);
        File file = new File(picturePath);
            Bitmap bmp = decodeFile(file,
    } catch (Exception e) {            

2. Working with Bitmaps in Android

Resize (scale) bitmap for given Height and Width:

public static Bitmap decodeFile(File file, int reqWidth, int reqHeight){
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(file.getPath(), options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(file.getPath(), options);     
private static int calculateInSampleSize(
    BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;

    return inSampleSize;

3. Get height and width of display before setting the bitmap:

DisplayMetrics metrics = appContext.getResources().getDisplayMetrics();
int height = metrics.heightPixels; 
int width = metrics.widthPixels;

4. Set the bitmap:

Bitmap bmp = decodeFile(file,width,height);
myWallpaperManager.suggestDesiredDimensions(width, height);


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Amol Kakhandki
Software Developer (Senior)
Netherlands Netherlands
No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 30 Jul 2013
Article Copyright 2013 by Amol Kakhandki
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid