0
我是開發Android應用程序的初學者。我正嘗試使用線程將圖像從an URL加載到ImageView
。這個想法是我想在不中斷整個過程的情況下加載圖像。下載後ImageView不更改圖像
問題是ImageView
下載完成後未加載圖像;我必須點擊主頁按鈕才能使ImageView
加載圖像。
我的代碼有錯嗎?
public class imageActivity extends Activity {
private ImageView imgView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgView =(ImageView)findViewById(R.id.img);
new Thread() {
@Override
public void run() {
try {
Drawable drawable = LoadImageFromWebOperations("http://www.androidpeople.com/wp-content/uploads/2010/03/android.png");
imgView.setImageDrawable(drawable);
} catch (Exception e) {
Log.e("TAG","" + e);
}
}
}.start();
}
private Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
System.out.println("Exc=" + e);
return null;
}
}
}
我建議你爲這個問題添加一個語言標籤以獲得更好的回覆 –