我開發一個應用程序,我在其中的每一行都有一個圖像列表,
此圖片是從網址加載的。下面
是我的代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/list_row_img"
android:layout_width="200dp"
android:layout_height="200dp"
/>
<TextView
android:id="@+id/list_row_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
,這是活動代碼
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img= (ImageView) findViewById(R.id.list_row_img);
Bitmap bm = null;
try {
URL aURL = new URL("http://image10.bizrate-images.com/resize?sq=60&uid=2216744464");
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
img.setImageBitmap(bm);
bis.close();
is.close();
} catch (Exception e) {
Log.v("EXCEPTION", "Error getting bitmap", e);
}
}
,當我跑我得到的設備上什麼都沒有。只有一個灰色的屏幕,並沒有發生異常。
注意,我在清單文件
<uses-permission android:name="android.permission.INTERNET" />
加入此權限任何人都可以幫我嗎?
感謝您的幫助。我有一個問題,請。 OpenHttpConnection(URL)此行有錯誤 是否有需要導入的API或庫? –
@RanaOsama不,不需要任何API或庫。 –
@RanaOsama請看我更新的導入包的答案。 –