【发布时间】:2013-11-20 08:26:48
【问题描述】:
我是安卓新手。我从下面的互联网获取代码
View.OnClickListener loadImage = new View.OnClickListener(){
public void onClick(View view) {
loadImage(imageLocation);
}
};
void loadImage(String image_location){
URL imageURL = null;
try {
imageURL = new URL(image_location);
}
catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection connection= (HttpURLConnection)imageURL.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);//Convert to bitmap
Bitmap drawableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
drawView.setCanvasBitmap(drawableBitmap);
}
catch (IOException e) {
e.printStackTrace();
}
}
单击按钮时,此方法将从 URL 加载图像。它可以工作,但我需要在活动启动时从 URL 加载图像。
我应该如何修改代码以删除 onClick ?请寻求帮助。谢谢=)
【问题讨论】:
-
你听说过
Activity的生命周期吗?首先检查developer.android.com/reference/android/app/Activity.html -
我认为你应该在尝试从 URL 加载图像之前阅读一些关于 developer.android.com 的文档...
-
此代码在您的应用程序中的什么位置,是在活动中还是在其他地方?
标签: android android-imageview android-view android-image