我必須製作一個應用程序,我正在從URL下載圖像,並且必須在縮放後顯示圖像。縮放圖像在Android中不可見
圖像的默認大小爲220x200
縮放圖像的大小爲55x50
這裏是我做的,上面的代碼:
XML文件格式:
<ImageView android:id="@+id/productimage"
android:layout_gravity="center_vertical|left" android:layout_width="55dip"
android:layout_height="50dip" android:adjustViewBounds="true" />
下面是下載圖像並調整其大小的代碼:
ImageView productImage = (ImageView) arg1
.findViewById(R.id.productimage);
if (!headerDetails[1].equals("null")) {
Bitmap image = getImage(headerDetails[1]);
if (image != null) {
try {
productImage
.setImageBitmap(Bitmap
.createScaledBitmap(
image,
productImage
.getMeasuredWidth(),
productImage
.getMeasuredHeight(),
false));
} catch (Exception e) {
e.printStackTrace();
}
}
}
下面是下載圖片類:
private Bitmap getImage(String address) {
try {
Log.i("getimage", address);
URL url = new URL(address);
URLConnection conn = url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(bis);
// bis.close();
// is.close();
return bm;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
沒有異常被拋出在這裏,因此圖像被正確下載。
輸出:在圖像視圖中不顯示任何圖像。
我做錯了什麼。我想要的圖像大小是55x50,但網上圖像的大小可能會有所不同。
謝謝你提前。
編輯:
以下代碼後形成的位圖:
Bitmap bm = BitmapFactory.decodeStream(bis);
不具有高度和寬度,它的高度和寬度被設置爲-1。這裏可能會出現什麼問題?
以下異常也被拋出:
06-23 14:24:41.635: WARN/System.err(16618): java.lang.IllegalArgumentException: width and height must be > 0
嘗試過,正在顯示沒有圖像。 – user590849