可能重複:
Download a file with Android, and showing the progress in a ProgressDialog從網站崩潰負荷圖像
我試圖從網站加載圖像,我看到了這樣一個問題:Android load from URL to Bitmap但我的應用程序崩潰,在這條線: conn.connect();
public class HTTPTest extends Activity {
ImageView imView;
String imageUrl = "http://api.androidhive.info/images/sample.jpg";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Button bt3 = (Button) findViewById(R.id.get_imagebt);
bt3.setOnClickListener(getImgListener);
imView = (ImageView) findViewById(R.id.imview);
}
View.OnClickListener getImgListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
downloadFile(imageUrl);
}
};
Bitmap bmImg;
void downloadFile(String fileUrl) {
URL myFileUrl = null;
try {
myFileUrl = new URL(fileUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
imView.setImageBitmap(bmImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
的main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, HTTPImage load test"
/>
<Button
android:id="@+id/get_imagebt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get an image"
android:layout_gravity="center"
/>
<ImageView
android:id="@+id/imview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
我的清單:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.androidtest.HTTPTest"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<!-- Permission to write to external storage -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
在此先感謝您的幫助!
Germain。
我只能猜測,例外是NetworkOnMainThread ... – Sam
哦的確是的。我該如何解決這個問題? 我的目錄下載: 11-19 15:12:52.655:E/AndroidRuntime(21823):致命異常:主 11-19 15:12:52.655:E/AndroidRuntime(21823):android.os.NetworkOnMainThreadException 11-19 15:12:52.655:E/AndroidRuntime(21823):\t at android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1178) .. – GermainGum
將來總是將LogCat添加到您的問題中應用程序崩潰,這是幫助我們解決錯誤的路線圖。 – Sam