如何从Android中的URL显示的图像 [英] How to show an image from an url in android
问题描述
我已经看过了互联网附近,但一直没能找到一个工作例如如何从一个网址显示的图像。
下面code崩溃,我无法找出原因。任何帮助是极大的AP preciated。谢谢你。
TesteImages.java:
包pac.image3;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.net.MalformedURLException;
进口的java.net.URL;
进口android.app.Activity;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.os.Bundle;
进口android.widget.ImageView;
公共类TestImages延伸活动{
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
//的setContentView(R.layout.main);
ImageView的形象=(ImageView的)findViewById(R.id.test_image);
字符串IMAGEURL =http://www.small-world.net/_borders/small_world_logo.gif;
尝试 {
ImageView的I =(ImageView的)findViewById(R.id.test_image);
点阵位图= BitmapFactory.de codeStream((InputStream的)新的URL(IMAGEURL).getContent());
i.setImageBitmap(位);
的setContentView(图像);
}赶上(MalformedURLException异常E){
e.printStackTrace();
}赶上(IOException异常E){
e.printStackTrace();
}
}
}
RES /布局/ main.xml中:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>
<的TextView
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=@字符串/你好
/>
< ImageView的
机器人:ID =@ + ID / test_image
机器人:SRC =@可绘制/图标
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
/>
< / LinearLayout中>
只需使用下面的方法来从URL绘制图像:
绘制对象drawable_from_url(URL字符串,字符串src_name)抛出
java.net.MalformedURLException,产生java.io.IOException
{
返回Drawable.createFromStream(((java.io.InputStream中)
新的java.net.URL(URL).awagetContent()),src_name);
}
只是通过字符串URL的方法(和src_name任何字符串),它会回报你一个绘制对象,然后使用 setBckgroundDrawable() ImageView的方法来设置图像的背景。
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:
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:
<?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="@drawable/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
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);
}
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显示的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!