0

I'm currently trying to download an picture via URL and put it in a TextView using "fromHtml" in my Android App. I'm using a TextView because of a slidepanel. I need to have text and picture at the same time, trying to have a good template.

I'm getting the right image actually but I don't understand why is it so small. The picture i'm trying to download is on a wamp server i've made ans its a 400x300 picture.

I'm using a piece of code I've found on StackOverflow to get image from a url which implement ImageGetter and the getIntrinsicWidth() and getIntrinsicHeight() methods but they are dividing the picture size by 4 !

I've print these two results, IntrinsicWidth returns 100 and IntrinsicHeight returns 75... So it displays a very small picture in my application and I really don't know why and I can't figure it out...

I tried using a 7360x4912 image and with this size I'm able to get a proper image size in my TextView (but still divided by 4 so I get a 1840x1228 image).

I also tried to multiply those values by 4 but it's just enlarging the container and not the content...

Here's a sample of my java code :

@Override
    protected void onPostExecute(Drawable result) {
        // set the correct bound according to the result from HTTP call
        Log.d("IMAGE DEBUG", ""+result.getIntrinsicWidth());
        Log.d("IMAGE DEBUG", ""+result.getIntrinsicHeight());
        int width = result.getIntrinsicWidth();
        int height = result.getIntrinsicHeight();

        urlDrawable.setBounds(0, 0, width, height);

        // change the reference of the current drawable to the result
        // from the HTTP call
        urlDrawable.drawable = result;

        URLImageParser.this.container.setMinimumHeight(result.getIntrinsicHeight());
        URLImageParser.this.container.requestLayout();

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

    /***
     * Get the Drawable from URL
     * @param urlString
     * @return
     */
    public Drawable fetchDrawable(String urlString) {
        try {
            Log.d("IMAGE DEBUG", "" + 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;
        }
    }

And here is where I use the picture I get :

TextView panel = (TextView) findViewById(R.id.slidingpanelview);
            URLImageParser p = new URLImageParser(panel, getApplicationContext());

            String name = queryResults.get(mHashMap.get(marker)).getName();
            String address = queryResults.get(mHashMap.get(marker)).getAddress();
            String phonenumber = queryResults.get(mHashMap.get(marker)).getPhoneNumber();

            panel.setText("");
            String titleDisplay = "<table><tr><h2 align=\"center\">"+name+"</h2></tr>";
            String addressDisplay = "<tr><h3 align=\"left\">Address</h3><p>"+address+"</p></tr>";
            String phonenumberDisplay = "<tr><h3 align=\"left\">Phone number</h3><p>"+phonenumber+"</p></tr>";
            String space ="<tr></tr>";
            String imageDisplay = "<tr><img src=\"http://ip_address_from_server/img/"+name+".jpg\"></tr></table>";
            Spanned htmlSpan = Html.fromHtml(titleDisplay + addressDisplay + phonenumberDisplay + space + imageDisplay, p, null);
            //Spanned htmlSpan = Html.fromHtml(titleDisplay + addressDisplay + phonenumberDisplay + space + imageDisplay);
            panel.setText(htmlSpan);

Here is what it looks like with the 400x300 image in my android App : 400x300 result

And here is what it looks like with the 7360x4912 image : 7360x4912 result

So basically I'm trying to have text and image at the same time in a slide panel, so if you have some ideas on how to patch this or if you know another way to do this it will be great.

Thanks in advance !

Lucien
  • 15
  • 6

1 Answers1

0

The getIntrinsicWidth and getIntrinsicHeight methods return the size the drawable SHOULD be AFTER Android scales the drawable. Creating a drawable programmatically creates a default mdpi drawable and so, if you are then running this application on an xxxhdpi device, this explains your 4x scale factor.

Here is a better explanation of how getIntrinsic functions actually work

Community
  • 1
  • 1
D Yao.
  • 391
  • 1
  • 6
  • Oh I see ! Thank you very much for this explanation ! Actually it's true, i'm developing this app directly on my Samsung Galaxy S6 which has a 2560 x 1440 resolution. So I have to find how I can change the metrics from the drawable I get ? – Lucien Feb 25 '16 at 22:37
  • Yep, you can get the density by calling `getResources().getDisplayMetrics().density` – D Yao. Feb 26 '16 at 15:42