0

I want to use higher size images for tablets and to decrease the size for phones.
I am doing this for all images in ImageButton.
I have tried placing different size images like hdpi, ldpi, mdpi etc in different folders inside drawable but this is also not working.

I have read that different layouts should be used, but in newer API Levels it is now deprecated.

Is there any easy way of doing this?

Mukesh Thawani
  • 316
  • 5
  • 12

3 Answers3

0

Based on you Phone/Tablet pixel density , Android system will automatically choose the apt image for your Phone/Tablet. But still, you can explicitly make Android choose different size images. Like this..

// for Phones drawable-ldpi drawable-mdpi drawable-hdpi

//for 7 inch tablets drawable-large-mdpi drawable-large-hdpi(for Nexus 7)

// for 10 inch tablets drawable-xlarge-mdpi

res/drawable-xlarge-mdpi/

Prabhuraj
  • 928
  • 2
  • 8
  • 14
0

Android will choose which one it will need. So you just have to place images with good sizes on good folders.
You can check the documentation here, you also can check this post.

Community
  • 1
  • 1
0

This is one of the code I found online to reduce the size of the image. Try using this.

    import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

public class AndroidLoadImageViewActivity extends Activity {

 String imagefile ="/sdcard/IMG_9331.JPG";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView image = (ImageView)findViewById(R.id.image);
        //Bitmap bm = BitmapFactory.decodeFile(imagefile);
        Bitmap bm = ShrinkBitmap(imagefile, 300, 300);
        image.setImageBitmap(bm);
    }

 Bitmap ShrinkBitmap(String file, int width, int height){

     BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
        bmpFactoryOptions.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

        int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
        int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);

        if (heightRatio > 1 || widthRatio > 1)
        {
         if (heightRatio > widthRatio)
         {
          bmpFactoryOptions.inSampleSize = heightRatio;
         } else {
          bmpFactoryOptions.inSampleSize = widthRatio; 
         }
        }

        bmpFactoryOptions.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
     return bitmap;
    }
}
Prabhuraj
  • 928
  • 2
  • 8
  • 14