How to increase the width of bitmap image?

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

How to increase the width of bitmap image?

Postby androidrocks » Thu Apr 02, 2009 4:03 pm

string url1 = "my url";
URL url = new URL(url1);
URLConnection conn = url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
BitmapFactory.Options bf = new BitmapFactory.Options();
bm = BitmapFactory.decodeStream(bis);
imgView.setImageBitmap(bm);

I am using this code to get the image from web and set it on imageview . But the issue is the width that i get is the same as original image. I want to increase the width of the image so that it automatically fits as per the screen size. Any idea on how to increase the width of the bitmap image.

When I use drawable it automatically fits the same image as the screen size. I use drawable.setBackground(drawableimage);

But this is not the case with bitmap image. Any idea on how to increase the width of the bitmap image.
androidrocks
Freshman
Freshman
 
Posts: 6
Joined: Tue Mar 24, 2009 8:47 am

Top

Postby stifry » Thu Apr 02, 2009 9:27 pm

Here you go. This scales it down, but you can modify it to scale up.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.trackaroo.apps.mobile.android.Trackmaster.bitmap;
  3.  
  4.  
  5.  
  6. import java.io.InputStream;
  7.  
  8.  
  9.  
  10. import android.content.Context;
  11.  
  12. import android.graphics.Bitmap;
  13.  
  14. import android.graphics.BitmapFactory;
  15.  
  16. import android.net.Uri;
  17.  
  18.  
  19.  
  20. public class BitmapUtil {
  21.  
  22.  
  23.  
  24.         public static Bitmap getBitmap(Context context,String photoUriPath) throws Exception {
  25.  
  26.                 Uri photoUri = Uri.parse(photoUriPath);
  27.  
  28.                 InputStream photoStream = context.getContentResolver().openInputStream(photoUri);
  29.  
  30.                 BitmapFactory.Options options = new BitmapFactory.Options();
  31.  
  32.                 options.inSampleSize=2;
  33.  
  34.                 Bitmap photoBitmap = BitmapFactory.decodeStream(photoStream,null,options);
  35.  
  36.                 int h = photoBitmap.getHeight();
  37.  
  38.                 int w = photoBitmap.getWidth();
  39.  
  40.                 if((w>h)&&(w>128)){
  41.  
  42.                         double ratio = 128d/w;
  43.  
  44.                         w=128;
  45.  
  46.                         h=(int)(ratio*h);
  47.  
  48.                 }
  49.  
  50.                 else if((h>w)&&(h>128)){
  51.  
  52.                         double ratio = 128d/h;
  53.  
  54.                         h=128;
  55.  
  56.                         w=(int)(ratio*w);
  57.  
  58.                 }
  59.  
  60.                 Bitmap scaled = Bitmap.createScaledBitmap(photoBitmap, w, h, true);
  61.  
  62.                 photoBitmap.recycle();
  63.  
  64.                 return scaled;
  65.  
  66.         }
  67.  
  68. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
Visit Trackaroo.com!
Trackmaster - Motorsports Lap Timer
Dynomaster - Performance Dyno
Image Image
User avatar
stifry
Developer
Developer
 
Posts: 46
Joined: Tue Feb 17, 2009 7:50 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 9 guests