2016-05-16 58 views
0

我在我的應用程序中動態創建了imageview。我試圖從URL加載圖像。但它不起作用。我已經嘗試了很多方法,並且我目前使用的代碼是如何從android中的URL將圖像加載到動態創建的imageview中?

try { 
      Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png").getContent()); 
      img.setImageBitmap(bitmap); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

但是圖像未加載。什麼是錯誤?

+0

使用像Glide或Picasso或通用圖像加載器的圖像庫來加載圖像。它好得多 –

回答

1

嘗試piccaso庫:

從下面的網址0
ImageView imageView = (ImageView) findViewById(R.id.imageView); 

//加載圖像到ImageView的

Picasso.with(this).load("https://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png").into(imageView); 

//改變URL 起飛許可,您mainifest:

<uses-permission android:name="android.permission.INTERNET" /> 

調整大小:

Picasso.with(getApplicationContext()).load("https://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png").resize(100, 100).into(i1); 
+0

錯誤:找不到變量Picasso。我在編譯時遇到了這個錯誤。需要使用任何包? – bala

+0

你需要添加piccaso庫。在你的項目中使用這個jar picasso-2.5.2 –

+0

@bala檢查這個鏈接:http://mvnrepository.com/artifact/com.squareup.picasso/picasso/2.5.2 –

0

您可以使用通用圖像裝載機

UIL旨在爲圖像加載,緩存和顯示功能強大,靈活和高度可定製的儀器。它提供了很多配置選項,並且可以很好地控制圖像加載和緩存過程。

// Load image, decode it to Bitmap and display Bitmap in ImageView (or any other view 
// which implements ImageAware interface) 
imageLoader.displayImage(imageUri, imageView); 

參考鏈接 - https://github.com/nostra13/Android-Universal-Image-Loader

0

使用用於從互聯網加載圖像的簡單AQuery庫

// AQuery object 
private AQuery aquery; 

aquery = new AQuery(context); 

ImageView imageView = (ImageView) findViewById(R.id.imageView); 

aquery.id(imageView).image("Your image url ",false,false); 

Add permission to manifest file 

<uses-permission android:name="android.permission.INTERNET" /> 
相關問題