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);

    asyncTask.execute(source);

    return urlDrawable;
}

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

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

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

    @Override 
    protected void onPostExecute(Drawable result) { 


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

        urlDrawable.drawable = result; 

        // redraw the image by invalidating the container 
        URLImageParser.this.container.invalidate();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        {
             // For ICS
            URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight() 
                    + result.getIntrinsicHeight()));
        }    
        else 
        {
             // Pre ICS
            URLImageParser.this.container.setEllipsize(null);
        }

        container.setText(container.getText()); 

    } 


    public Drawable fetchDrawable(String urlString) {
        try 
        {
            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);
tvArticle.setText(htmlSpan);
tvArticle.setMovementMethod(LinkMovementMethod.getInstance()); 

      

Screenshots of my app and browser image:

enter image description here

enter image description here

+3


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.

0


source


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.

0


source


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;

    }

      

0


source







All Articles