2

I'm making an application for a blog. Articles in the blog may contain images in between text. To achieve this I used URLImageParser class which extends ImageGetter to bind Images to Textview. I'm able to append Images, but the size of the Image is varying.

When I open the Image in browser, I can see a larger pic, but in my application I'm getting smaller width and height. How do I adjust the width and height based on the 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 it to the TextView:

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

Screenshots of Image my app and Browser:

enter image description here

enter image description here

Anirudh
  • 2,767
  • 5
  • 69
  • 119
  • Better to display your content in `WebView` – M D Oct 13 '14 at 09:25
  • `How do I adjust the width and height based on the Image size from Browser.`. That is nonsense. Your app does know nothing of a browser. If you want your app to display images bigger then just tell your app to do so. – greenapps Oct 13 '14 at 09:30
  • @greenapps In fetchDrawable() method when I call drawable.getIntrinsicWidth() or drawable.getIntrinsicHeight() I'm not getting exact width and height of the Image. – Anirudh Oct 13 '14 at 09:41
  • Does not matter. If you use an ImageVoew you can tell your ImageView to display it in a wanted size. – greenapps Oct 13 '14 at 10:11
  • @greenapps I tried that, Used full screenwidth for Imageview width, but few images are getting distorted due to that. – Anirudh Oct 13 '14 at 10:24
  • I noticed that in the browser's url, you're adding a w=680 parameter. Are you including that when you load the image on the phone? – Gil Moshayof Nov 03 '14 at 08:56
  • Also, keep in mind that different devices have different density. The image which is being returned has a certain size in pixels, and so if a device has a high density, the picture may appear smaller, even though it has the same pixel size as the browser image. – Gil Moshayof Nov 03 '14 at 08:59
  • @GilMoshayof I have removed w=680 from the URL before passing it to ImageGetterAsyncTask. How do I handle different densities? – Anirudh Nov 03 '14 at 11:02

3 Answers3

0

First of all, in the browser version you're adding a "w=680" to the request, which probably makes the server process the image with 680 width. It's likely that if you do the same from the android version, you'll get an image which is closer to what you're expecting to receive.

Second of all, if you want your image to appear a specific size regardless of device density, then you'll need to resize the containing ImageView according to the size in dip (density-independant-pixels).

You can find the way to do this here

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

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

Community
  • 1
  • 1
Gil Moshayof
  • 16,633
  • 4
  • 47
  • 58
0

you have two options :

First Option

you display content in WebView which will handle the webpage at its own and you dont have to resize etc.

Second Option

you parse the source code of web page and find Image Dimensions Given in Source code of page and convert them to DP unit and apply it to ImageView.

Kirtan
  • 1,782
  • 1
  • 13
  • 35
0

Hi you can use below method to know height and width of the image at 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;

    }
Piyush
  • 1,973
  • 1
  • 19
  • 30