I am trying to download an Image and then display it to my imageView component.
To download I have to use Asyntask and display a progress bar to inform a user. Problem is after going through a loop to get the calculated progress value I get 0 from inputStream.
Log.d("is", "" + inputStream.available()); // ---> will have a value
byte[] buffer = new byte[contentLenght];
while ((read = inputStream.read(buffer)) != -1) {
counter += read;
publishProgress(counter);
outputStream.write(buffer,0,read);
}
Log.d("is", "" + inputStream.available()); // -----> will return 0
bmp = BitmapFactory.decodeStream(inputStream); // bmp will be empty
Is there a way to get the calcuated value for progress bar and not get a 0 value at the end in input stream?
I am using Asyntask here.
bmp will have a value and when I do this
imageView.setImageBitmap(bmp);
it will work ONLY IF i remove the the loop and just call bmp = BitmapFactory.decodeStream(inputStream);
However if I put a loop before doing this
bmp = BitmapFactory.decodeStream(inputStream);
the imageView will show nothing
Here is my Full Asynctask Code Including the networking connection
int progressCounter;
int contentLenght;
int counter;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected Boolean doInBackground(String... params) {
return ConnectToInternet(params[0]);
}
@Override
protected void onPostExecute(Boolean aVoid) {
//Log.d("buff",bmp.toString());
progressBar.setVisibility(View.GONE);
imageView.setImageBitmap(bmp);
}
@Override
protected void onProgressUpdate(Integer... values) {
progressCounter =(int) (((double) values[0] / contentLenght) * 100);
progressBar.setProgress(progressCounter);
}
boolean ConnectToInternet(String url){
boolean sucessfull = false;
URL downloadURL = null;
HttpURLConnection connection = null;
InputStream inputStream = null;
try {
downloadURL = new URL(url);
connection = (HttpURLConnection) downloadURL.openConnection();
inputStream = connection.getInputStream();
contentLenght = connection.getContentLength();
Log.d("is", "" + inputStream.available());
int read = -1;
byte[] buffer = new byte[contentLenght];
while ((read = inputStream.read(buffer)) != -1) {
counter += read;
publishProgress(counter);
}
Log.d("is", "" + inputStream.available());
bmp = BitmapFactory.decodeStream(inputStream);
sucessfull = true;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
connection.disconnect();
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sucessfull;
}
thanks
The while
statement is consuming the inputStream
entirely, so nothing will be left for decoding in BitmapFactory.decodeStream(inputStream)
.
Try this:
boolean ConnectToInternet(String url){
// ...
int read;
// contentLength may be too big,
// so read stream in smaller chunks.
//
// there's a typo in contentLenght :)
byte[] buffer = new byte[4096];
// Object for storing partially downloaded image.
ByteArrayOutputStream imageBaos = new ByteArrayOutputStream();
// Initialize counter.
counter = 0;
while ((read = inputStream.read(buffer)) != -1) {
counter += read;
publishProgress(counter);
// Store downloaded chunk.
imageBaos.write(buffer, 0, read);
}
// Obtain bitmap from downloaded chunks.
bmp = BitmapFactory.decodeByteArray(imageBaos.toByteArray(), 0, imageBaos.size());
// ...
}