user3391191
user3391191

Reputation: 23

Android Image Resizing

I am downloading images from web URL and showing on my android application but I am not able to resize my image according to my requirements.

private Bitmap decodeFile(File f) {
    try {
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        FileInputStream stream1 = new FileInputStream(f);
        BitmapFactory.decodeStream(stream1, null, o);
        stream1.close();

        //Find the correct scale value. It should be the power of 2.

        // Set width/height of recreated image
        final int REQUIRED_SIZE = 285;

        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
                break;
            width_tmp /= 1;
            height_tmp /= 1;
            scale *= 2;
        }
        //decode with current scale values
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        FileInputStream stream2 = new FileInputStream(f);
        Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2);
        stream2.close();
        return bitmap;
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;


}

now can any one tell me how can i resize my images to 250*250 px also thanx in advance

Upvotes: 0

Views: 98

Answers (1)

omriherman
omriherman

Reputation: 254

Try and use Picasso library - http://square.github.io/picasso/.

It is fairly easy to use and integrate, and has a resize method doing exactly this.

Picasso.with(context).load(url).resize(250, 250).centerCrop().into(imageView);

you basically put instead of 'url' your url.

Upvotes: 1

Related Questions