在加载图像时添加进度对话框的Android帮助?
我一直在遵循一个教程,远程下载图像到imageview,但我不知道如何添加一个进度对话框(图像或其他东西)来向用户显示正在下载的图像,而不仅仅是一个空白屏幕 希望有人能帮忙在加载图像时添加进度对话框的Android帮助?,android,dialog,imageview,progress,Android,Dialog,Imageview,Progress,我一直在遵循一个教程,远程下载图像到imageview,但我不知道如何添加一个进度对话框(图像或其他东西)来向用户显示正在下载的图像,而不仅仅是一个空白屏幕 希望有人能帮忙 ImageView imView; String imageUrl="http://domain.com/images/"; Random r= new Random(); /** Called when the activity is first created. */ @Override public void
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();
}
}
}
您应该看到这一点:asynctask是一个不错的选择 问候,, Stéphane您需要查看类,基本上在加载图像时使用它。或者,您可以在下载图像时放置默认图像 要将进度条放入代码中,最简单的方法基本上就是翻转它们在布局中的可见性
完全重复谢谢Stephane,但我是一个android新手,您提供的链接只是技术链接,我可以管理。我想,如果我能将这个进度对话框应用到我已经在使用的代码中,这会对我有所帮助,这样至少我可以了解每件事情是如何工作的。您是否可以告诉我如何在我提供的代码中实现它。谢谢你,LucyThanks Momo,谢谢你的深入回答。非常感谢。现在一切听起来都有点过头了。(新手在这里发抖)我不太确定从哪里开始。我将仔细阅读您的笔记和建议,并尝试逐一实施。你在笔记中提到要从android获取进度条,我在哪里可以找到?对不起,如果我听起来很无知,我真的很好,当我不是在努力学习android的时候!再次感谢..Luci,这是到ProgressBar的链接,您可以在下载图像时将其放入布局并显示出来
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;
}
}