I have been following a tutorial that remotely downloads an image to an imageview, but i’m not sure how to add a progress dialog (image or something) to show the user that image is downloading, instead of just a blank screen.
Hope someone can help
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();
}
}
}
You need to look at ProgressBar class and basically use that while the image is loading. Alternatively you could put default image while the image is being downloaded.
To put the progress bar in your code, the easiest basically just flip their visibility in the layout.
Here is what you should try to do. Check the NOTE and TODO comment in the code. Note: I just modified your code, but haven’t run it, but this should be enough to illustrate the idea.
Some key points: