AndroidDev
AndroidDev

Reputation: 4559

How to decode image file

I have an image of height 200 and 400 pixels. The way I want to display all these images is in height of 200 pixels. I mean to say whatever the size of an image, while displaying that image I want to display image up to a height of 200 pixels. The rest portion of the image is hidden. So how that can be done? I have used one code for decoding but here it stretches the bigger size image and then displays it. In my case, I don't want the image to stretch but only display images up to height 200 pixels.

Code I used:

private Bitmap decodeFile(File f)
{
    try 
    {
        // decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        
        // Find the correct scale value.
        final int REQUIRED_SIZE = 200;
        int height_tmp = o.outHeight;
        while(true)
        {
            if(height_tmp/2 < REQUIRED_SIZE)
                break;          
            height_tmp/=2;
        }

        o.inSampleSize = height_tmp;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o);
    } 
    catch (FileNotFoundException e) 
    {}
    return null;
}

Upvotes: 0

Views: 618

Answers (2)

jeet.chanchawat
jeet.chanchawat

Reputation: 2575

Ok, So try layoutParams for doing the same task programatically.

  public void taskCompleted ()
{

    ImageView iv = new ImageView(this);


    Bitmap completionBitmap = null;
    completionBitmap = BitmapFactory.decodeFile(CommonMessageConstants.BASE_IMAGE_FOLDER_ON_DEVICE +"completionimage.png");


    iv.setImageBitmap(completionBitmap);
    RelativeLayout rl = (RelativeLayout) findViewById(R.id.randomImageView);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    //lp.setMargins(200, 200, 0, 0);

    lp.addRule(RelativeLayout.CENTER_IN_PARENT);


    rl.addView(iv, lp);

    Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_SHORT).show ();

} 

Upvotes: 1

jeet.chanchawat
jeet.chanchawat

Reputation: 2575

just edit your main.xml

    android:layout_width="200dp"
    android:layout_height="200dp"

Upvotes: 1

Related Questions