我一直在关注将图像远程下载到imageview的教程,但我不知道如何添加进度对话框(图像或其他内容)来向用户显示图像正在下载,而不仅仅是空白屏幕
希望有人可以提供帮助
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();
}
}
}
答案 0 :(得分:1)
答案 1 :(得分:0)
您需要查看ProgressBar类,并在图像加载时基本使用它。或者,您可以在下载图像时放置默认图像。
要将进度条放在代码中,最简单的方法就是在布局中翻转它们的可见性。
这是你应该尝试做的。检查代码中的NOTE和TODO注释。注意:我刚刚修改了你的代码,但没有运行它,但这应该足以说明这个想法。
一些关键点:
View.OnClickListener getImgListener = new View.OnClickListener()
{
@Override
public void onClick(View view) {
// NOTE: here you need to show the progress bar, you could utilize ProgressBar class from Android
// TODO: Show progress bar
AsyncTask asyncTask = new AsyncTask() {
@Override
public Bitmap doInBackground(Void... params) {
int i =r.nextInt(114);
// NOTE: move image download to async task
return downloadFile(imageUrl+"image-"+i+".jpg");
}
@Override
public void onPostExecute(Bitmap result) {
// TODO: hide the progress bar here
// and flip the image to VISIBLE as noted above
if(result != null) {
imView.setImageBitmap(result);
} else {
// NOTE 3: handle image null here, maybe by showing default image
}
}
};
// NOTE: execute in the background so you don't block the thread
asyncTask.execute();
}
};
// Change the return type to Bitmap so we could use it in AsyncTask
Bitmap downloadFile(String fileUrl){
URL myFileUrl =null;
try {
myFileUrl= new URL(fileUrl);
} catch (MalformedURLException e) {
// NOTE: You should not have e.printStacktrace() here. In fact
// printStacktrace is a bad practice as it doesn't really do anything
// 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();
Bitmap bmImg = BitmapFactory.decodeStream(is);
// return image this to the main Thread
return bmImg;
} catch (IOException e) {
return null;
}
}
答案 2 :(得分:0)
另一个主题:
How to display Progressbar When data is loading from the web?,Blundell的答案,只需复制/粘贴课程。
我在ListView中使用它,它非常好。
您可以在xml中或在View创建期间或通过调用setImageDrawable(String)