How to get the exact dimensions of an image of an image from an image url in Android?

I am applying for a blog. Blog articles can contain images between text. For this, I used the URLImageParser class, which extends ImageGetter to bind images to a Textview. I can add images, but the image size changes.

When I open the image in a browser, I see a larger image, but in my application, I get a smaller width and height. How to adjust width and height based on image size from browser.

URLImageParser class:

public class URLImageParser implements ImageGetter {
Context c;
TextView container;
int width;

public URLImageParser(TextView t, Context c) {
    this.c = c;
    this.container = t;


public Drawable getDrawable(String source) {
    URLDrawable urlDrawable = new URLDrawable();

    ImageGetterAsyncTask asyncTask = new ImageGetterAsyncTask(urlDrawable);


    return urlDrawable;

public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable>  {
    URLDrawable urlDrawable;

    public ImageGetterAsyncTask(URLDrawable d) {
        this.urlDrawable = d;

    protected Drawable doInBackground(String... params) {
        String source = params[0];
        return fetchDrawable(source);

    protected void onPostExecute(Drawable result) { 

        urlDrawable.setBounds(0, 0, 0+width, 0+result.getIntrinsicHeight());  

        urlDrawable.drawable = result; 

        // redraw the image by invalidating the container 

             // For ICS
                    + result.getIntrinsicHeight()));
             // Pre ICS



    public Drawable fetchDrawable(String urlString) {
            InputStream is = fetch(urlString);
            Drawable drawable = Drawable.createFromStream(is, "src");
            drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0 + drawable.getIntrinsicHeight()); 
            return drawable;
        } catch (Exception e) {
            return null;

    private InputStream fetch(String urlString) throws MalformedURLException, IOException {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet(urlString);
        HttpResponse response = httpClient.execute(request);
        return response.getEntity().getContent();



Binding to TextView:

URLImageParser p = new URLImageParser(tvArticle, this);
Spanned htmlSpan = Html.fromHtml(x+y, p, null);


Screenshots of my app and browser image:

enter image description here

enter image description here


source to share

3 answers

First of all, in the browser version, you add the request "w = 680", which probably forces the server to process an image with a width of 680. Probably if you do the same with the Android version, you will get an image that is closer to what you expect to receive.

Second, if you want your image to display at a specific size regardless of device density, you need to resize the containing ImageView

to match the size in dip (density-independent pixels).

You can find a way to do it here

Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sizeInDp, r.getDisplayMetrics()); 


Then with the px parameter you can resize the bitmap accordingly, or perhaps even request an image of that size directly from the server.



you have two options:

First option

you are showing content in a WebView that will render the web page on its own and you don't have to resize etc.

Second option

you parse the source code of the web page and find the image dimensions given in the source code of the page and convert them to a DP block and apply to the ImageView.



Hi you can use below method to find out height and width of image by url.

private void getDropboxIMGSize(Uri uri){
       BitmapFactory.Options options = new BitmapFactory.Options();
       options.inJustDecodeBounds = true;
       BitmapFactory.decodeFile(uri.getPath()).getAbsolutePath(), options);
       int imageHeight = options.outHeight;
       int imageWidth = options.outWidth;





All Articles