【发布时间】: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