我一直在关注一个远程下载图像到图像视图的教程,但我不知道如何添加进度对话框(图像或其他)来显示用户图像正在下载,而不是一个空白的屏幕。Android帮助在图像加载时添加进度对话框?
希望有人能帮助
ImageView imView;
String imageUrl="http://domain.com/images/";
Random r= new Random();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN ,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.galleryshow);
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
int i =r.nextInt(114);
downloadFile(imageUrl+"image-"+i+".jpg");
Log.i("im url",imageUrl+"image-"+i+".jpg");
}
};
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();
int length = conn.getContentLength();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
imView.setImageBitmap(bmImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
准确重复http://stackoverflow.com/questions/2735102/ideal-way-to-cancel-an-executing- asynctask/2740204#2740204 – Dharmendra