我使用以下两个功能从互联网下载和图像。当在图像视图中显示下载图像时,它的大小(宽度和高度(与实际大小不同。为什么?因为我已将图像保存到可绘制文件夹并在图像视图中显示它看起来像它的大小一样好。
爪哇代码:
public InputStream OpenHttpConnection(String urlString) throws IOException{
InputStream in = null;
int respobse = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if(!(conn instanceof HttpURLConnection))
throw new IOException("It's not HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
respobse = httpConn.getResponseCode();
enableHttpResponseCache();
if(respobse == HttpURLConnection.HTTP_OK){
in= httpConn.getInputStream();
Log.d("HTTP connection","OK");
}
}catch (IOException e) {
Log.e("HTTP ERROR",e.toString());
throw new IOException();
}
return in;
}
public Bitmap DownloadImage(String URL){
Bitmap bitmap = null;
InputStream in = null;
try{
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
}catch(IOException ex){
}
return bitmap;
}
包含图像视图的布局是:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/include21"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="@drawable/round" >
<ImageView
android:id="@+id/imgRecibeImage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/round"
android:scaleType="fitStart"
android:src="@drawable/img1" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right" >
<TextView
android:id="@+id/txtRecibeName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#80000000"
android:gravity="right"
android:padding="8dp"
android:text="Crispy Chicken and Sandwich"
android:textColor="#ffffff"
android:textSize="17sp" />
<TextView
android:id="@+id/tvLoader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/txtRecibeName"
android:layout_alignBottom="@+id/txtRecibeName"
android:layout_alignParentLeft="true"
android:layout_marginLeft="40dp"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</FrameLayout>