下面是使用到的三個文件:
為什麼顯示不了圖片呢?
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
package="com.imooc.summenudemo"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="18" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="com.imooc.summenudemo.MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Light.NoTitleBar"
>
android:name="com.imooc.summenudemo.ImageActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Light.NoTitleBar"
>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
image_view.xml
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="顯示" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image" />
ImageActivity.java
package com.imooc.summenudemo;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;
public class ImageActivity extends Activity {
/** Called when the activity is first created. */
String imageUrl = "http://content.52pk.com/files/100623/2230_102437_1_lit.jpg";
Bitmap bmImg;
ImageView imView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imView = (ImageView) findViewById(R.id.image);
imView.setImageBitmap(returnBitMap(imageUrl));
}
public Bitmap returnBitMap(String url){
URL myFileUrl = null;
Bitmap bitmap = null;
try {
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
}
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="顯示" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image" />