【问题标题】:Getting small size pictures from url Android从 url Android 获取小尺寸图片
【发布时间】:2016-06-08 22:31:53
【问题描述】:

我目前正在尝试通过 URL 下载图片,并在我的 Android 应用程序中使用“fromHtml”将其放入 TextView。由于滑动面板,我正在使用 TextView。我需要同时有文字和图片,试图有一个好的模板。

我实际上得到了正确的图像,但我不明白为什么它这么小。 我要下载的图片位于我制作的 wamp 服务器上,它是 400x300 的图片。

我正在使用我在 StackOverflow 上找到的一段代码从实现 ImageGetter 以及 getIntrinsicWidth() 和 getIntrinsicHeight() 方法的 url 获取图像,但它们将图片大小除以 4!

我打印了这两个结果,IntrinsicWidth 返回 100,IntrinsicHeight 返回 75...所以它在我的应用程序中显示了一个非常小的图片,我真的不知道为什么,我无法弄清楚...

我尝试使用 7360x4912 的图像,使用这个尺寸,我可以在我的 TextView 中获得合适的图像尺寸(但仍然除以 4,所以我得到了 1840x1228 的图像)。

我也尝试将这些值乘以 4,但它只是扩大了容器而不是内容...

这是我的 java 代码示例:

@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;
        }
    }

这是我使用我得到的图片的地方:

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

这是我的 android 应用中 400x300 图像的样子: 400x300 result

这是 7360x4912 图像的样子: 7360x4912 result

所以基本上我试图在滑动面板中同时显示文本和图像,所以如果你有一些关于如何修补这个问题的想法,或者你知道另一种方法来做到这一点,那就太好了。

提前致谢!

【问题讨论】:

    标签: android image url download size


    【解决方案1】:

    getIntrinsicWidthgetIntrinsicHeight 方法返回可绘制对象应该在 Android 缩放可绘制对象之后的大小。以编程方式创建一个可绘制对象会创建一个默认的 mdpi 可绘制对象,因此,如果您随后在 xxxhdpi 设备上运行此应用程序,这将解释您的 4 倍比例因子。

    Here is a better explanation of how getIntrinsic functions actually work

    【讨论】:

    • 哦,我明白了!非常感谢您的解释!实际上这是真的,我直接在我的三星 Galaxy S6 上开发这个应用程序,它的分辨率为 2560 x 1440。所以我必须找到如何从我得到的可绘制对象中更改指标?
    • 是的,你可以拨打getResources().getDisplayMetrics().density获取密度
    猜你喜欢
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 2015-03-13
    • 1970-01-01
    • 2018-07-28
    相关资源
    最近更新 更多