动态填充的Andr​​oid ImageView的与外部资源 [英] Dynamically populate Android ImageView with outside resources

查看:116
本文介绍了动态填充的Andr​​oid ImageView的与外部资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能把静态图像

 < ImageView的
        机器人:ID =@ + ID / imageView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =0.67
        机器人:SRC =@可绘制/ static_image/>
 

成ImageView的其源可以被动态设置到的数据,是不是已经在res夹

即,我的申请有屏幕上的图标,但用于图标的实际图像是从外部服务器下载的,并且可以动态地改变。如何更新与在下载时所需的图像的ImageView的?我想要的东西功能,如:

 图片selectedImage = //从服务器获取

myImageView.setImage(selectedImage);
 

解决方案

乌尔的问题不明确。如果妳只是想有一个形象(也就是在某些URL)设置为图像视图,

  BMP位= getBitmapFromURL(UR URL这里);
imgview.setImageBitmap(BMP);
 

和写这个功能:

 公共静态位图getBitmapFromURL(字符串SRC){
        尝试 {

            网址URL =新的URL(SRC);
            HttpURLConnection的连接=(HttpURLConnection类)网址
                    .openConnection();
            connection.setDoInput(真正的);
            connection.connect();
            输入的InputStream = connection.getInputStream();
            位图MYBITMAP = BitmapFactory.de codeStream(输入);

            返回MYBITMAP;

        }赶上(例外前){

            返回null;
        }
 

How can I turn the static image

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.67"
        android:src="@drawable/static_image" />

into an ImageView whose source can be dynamically set to data that is not already in the res folder?

That is, my application has an icon on the screen but the actual image for the icon is downloaded from an outside server and can change dynamically. How do I update the ImageView with the desired image upon download? I want something functionally like:

Image selectedImage = //get from server

myImageView.setImage(selectedImage);

解决方案

Ur question is not clear. If u just wanna have an image(that is in some url) set to an image view,

Bitmap bmp=getBitmapFromURL(ur url here);
imgview.setImageBitmap(bmp);

and write this function:

public static Bitmap getBitmapFromURL(String src) {  
        try {

            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap mybitmap = BitmapFactory.decodeStream(input);

            return mybitmap;

        } catch (Exception ex) {

            return null;
        }

这篇关于动态填充的Andr​​oid ImageView的与外部资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆