Question

I want to show progressbar not progress dialog while downloading image using intentService. i have download the image successfully but now i want to show progressbar during the process of downloading.Some one please tell me logic how can i do that by giving me an example. Thanks in advance.

My current code is. MainActivity

public class MainActivity extends Activity {
 private ResponseReceiver receiver;
 public ProgressBar mprogressbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    IntentFilter filter = new IntentFilter(ResponseReceiver.ACTION_RESP);
    filter.addCategory(Intent.CATEGORY_DEFAULT);
    receiver = new ResponseReceiver();
    registerReceiver(receiver, filter);

    Button download=(Button)findViewById(R.id.downloadImge);
   // mprogressbar=(ProgressBar) findViewById(R.id.progressBar1);

    download.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent msgIntent = new Intent(MainActivity.this,SimpleIntentService.class);
            startService(msgIntent);


        }
    });

}

@Override
public void onDestroy() {
    this.unregisterReceiver(receiver);
    super.onDestroy();
}


public class ResponseReceiver extends BroadcastReceiver {
    public static final String ACTION_RESP = "com.example.intent.action.MESSAGE_PROCESSED";
    @Override
    public void onReceive(Context context, Intent intent) {

        byte[] bitmapdata=intent.getByteArrayExtra("byteArray");

       Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata .length);
       ImageView displyImg=(ImageView) findViewById(R.id.displayImge);
    //   mprogressbar.setVisibility(View.GONE);
       displyImg.setImageBitmap(bitmap);

       //result.setText(text);
    }

}

}

And here is my IntentService.class

public class SimpleIntentService extends IntentService {

Bitmap myBitmap;
byte[] byteArray;

public SimpleIntentService() {
    super("SimpleIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {

    String imgurl="http://daertube.files.wordpress.com/2012/06/islamic-wallpaper-15.jpg";
    try {
    URL url = new URL(imgurl);
    HttpURLConnection connection = (HttpURLConnection) url
            .openConnection();
    connection.setDoInput(true);
    connection.connect();
    InputStream input = connection.getInputStream();
    //path=input.toString();
    myBitmap = BitmapFactory.decodeStream(input);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    myBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byteArray = stream.toByteArray();


    } catch (IOException e) {
        e.printStackTrace();
        Log.e("error in geting url ", e.getMessage().toString());
    }

    Intent broadcastIntent = new Intent();
    broadcastIntent.setAction(ResponseReceiver.ACTION_RESP);
    broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
    broadcastIntent.putExtra("byteArray",byteArray );
    sendBroadcast(broadcastIntent);
}
}
Was it helpful?

Solution

Use AsyncTask instead of service. There is a method onProgressUpdate wich you can use to set progress of ProgressBar

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top