How to show an image from an url in android(如何在android中显示来自url的图像)
问题描述
我浏览了互联网,但找不到关于如何从 url 显示图像的工作示例.
I have looked around the internet but haven't been able to find a working example on how to show an image from an url.
下面的代码崩溃了,我不知道为什么.非常感谢任何帮助.谢谢.
The below code is crashing and I can't find out why.. Any help is greatly appreciated. Thanks.
TesteImages.java:
TesteImages.java:
package pac.image3;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
public class TestImages extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
ImageView image = (ImageView) findViewById(R.id.test_image);
String imageUrl = "http://www.small-world.net/_borders/small_world_logo.gif";
try {
ImageView i = (ImageView)findViewById(R.id.test_image);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
i.setImageBitmap(bitmap);
setContentView(image);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
res/layout/main.xml:
res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ImageView
android:id="@+id/test_image"
android:src="QGRyYXdhYmxlL2ljb24="
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
推荐答案
只要用下面的方法从url中画图:
Just use the following method to draw image from url:
Drawable drawable_from_url(String url, String src_name) throws
java.net.MalformedURLException, java.io.IOException
{
return Drawable.createFromStream(((java.io.InputStream)
new java.net.URL(url).awagetContent()), src_name);
}
只需将字符串 url 传递给方法(对于 src_name 任何字符串),它将返回一个可绘制对象,然后使用 imageview 的 setBckgroundDrawable() 方法设置图像的背景.
Just pass the string url to the method(and for src_name any string ) and it will return you a drawable object, then use setBckgroundDrawable() method of the imageview to set the background of the image.
这篇关于如何在android中显示来自url的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在android中显示来自url的图像


- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01