【问题标题】:use dynamic url and image in same imageView在同一个 imageView 中使用动态 url 和图像
【发布时间】:2014-10-25 11:02:40
【问题描述】:

我的布局中有一个 imageView,名称是 iv1

我想当用户离线时它会显示一个图像和一个默认网址的链接。 但是当用户上网时,我想从 url 获取图像,并使用动态 url 获取图像链接。

怎么做,有什么好的做法..请

【问题讨论】:

    标签: android image url dynamic view


    【解决方案1】:

    创建一个名为 ConnectionDetector 的不同类

       public class ConnectionDetector {
    
        private Context context ;
        WifiManager wifiManager ;
        static WifiInfo wifiInfo ;
    
        public final static String internetError = "No Internet Connection";
    
        public ConnectionDetector (Context context)
        {
            this.context = context ;
            wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            wifiInfo = wifiManager.getConnectionInfo();
        }
    
        public boolean isInternetConnected()
        {
            ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    
            if(connectivity != null)
            {
                NetworkInfo[] info = connectivity.getAllNetworkInfo();
    
                if (info != null)
                {
                    for(int i = 0; i < info.length ; i++)
                    {
                        if(info[i].getState() == NetworkInfo.State.CONNECTED)
                        {
                            return true;
                        }
                    }
                }
    
            }
    
            return false;
        }
    
    }
    

    现在从您的活动中执行以下操作,

    ConnectionDetector cd = new ConnectionDetector (getApplicationContext());
    boolean isConnected = cd.isInternetConnected();
    
    if (isConnected)
    {
        // Internet is present, here you can download the image from URL and set it to ImageView. If you're new to image downloading then better use picasso library. It's only 1 line of code. 
    }
    
    else
    {
        // No internet connection, you can display default image and url in your imageview
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-30
      • 1970-01-01
      • 2020-09-03
      • 2011-03-08
      相关资源
      最近更新 更多