2013-05-05 36 views
1

我的服务器上有一张图片。该图像的URL是这样的: http://www.mydomain.com/123456uploaded_image.jpg使用URL设置ImageView

我想将此图像设置为我的ImageView。这里是我试过的代码:

try{ 
      String url1 = myeventimagearray[position]; 
      URL ulrn = new URL(url1); 
      HttpURLConnection con = (HttpURLConnection)ulrn.openConnection(); 
      InputStream is = (InputStream) con.getInputStream(); 
      Bitmap bmp = BitmapFactory.decodeStream(is); 
      if (null != bmp) 
       iveventimg.setImageBitmap(bmp); 
      else 
       Log.i("Image","Not set"); 

     } catch(Exception e) { 
      e.printStackTrace(); 
     } 

当我尝试这一点,我的ImageView是空的,也就是说,它不设置图像视图和我得到了我的logcat这个系统错误:

java.lang.ClassCastException:libcore.net.http.FixedLengthInputStream不能转换到com.example.eventnotifier.Base64 $ InputStream的

Base46.java是我从网上得知编码找到的文件和解码并来自Base64符号。

任何想法,为什么我得到这个System.error?

谢谢

回答

1

使用URlImageViewHelper,很会照顾加载URL到ImageView的。

Refer this

很会照顾缓存,加载在后台等本身。

+0

非常感谢!它的工作:) – Nemin 2013-05-05 21:29:12

+0

干杯..快乐编码:) – 2013-05-05 21:42:49

0

你很可能得到一个例外,因为你正在尝试做在主线程网络IO。考虑使用加载器或AsyncTask加载图像。

检查你的日志,我打赌你正在自动生成的catch块中打印堆栈跟踪。

new Thread(new Runnable() { 
    public void run() { 
    final Bitmap b = bitmap = BitmapFactory.decodeStream((InputStream)new URL(myeventimagearray[position]).getContent()); 
    iveventimg.post(new Runnable() { 
     public void run() { 
     iveventimg.setImageBitmap(b); 
     } 
    }); 
    } 
}).start(); 
+0

我编辑过的文章给出了关于错误的详细解释。它是否仍然在主线程上做网络IO? – Nemin 2013-05-05 21:15:54

+0

您得到的例外是由于其他原因。 – 2013-05-05 21:25:37