yoshi24
yoshi24

Reputation: 3177

How to resize bitmap decoded from URL?

I am using bitmap to get a image from a url using this

public void loadImage(String url){
try {

       bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());

    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

}

Is there anyway i can resize the image from here? setting the width and the height of it still keeping the resolution?

Upvotes: 1

Views: 9233

Answers (4)

Lalit Poptani
Lalit Poptani

Reputation: 67286

You can try this too.

BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1; // 1 = 100% if you write 4 means 1/4 = 25% 
bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent(),
                                                                   null, bmOptions);
bmImage.setImageBitmap(bitmap);

Upvotes: 5

Franzé Jr.
Franzé Jr.

Reputation: 1194

You can just use Picasso(very simple!):

Picasso.with(context) .load(url) .resize(10, 10) .into(imageView)

http://square.github.io/picasso/

Upvotes: 1

Ryan_Chau
Ryan_Chau

Reputation: 273

why don't you set the width and height property of imageView in XML?

I find out that the image from url is resized automatically so that it can fit in that imageView.

Upvotes: 0

Luis Miguel Serrano
Luis Miguel Serrano

Reputation: 5099

Use: Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)

Upvotes: 7

Related Questions