0

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);
}
}
Numan Ahmad
  • 157
  • 1
  • 4
  • 14

1 Answers1

2

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

Maxim Efimov
  • 2,747
  • 1
  • 19
  • 25
  • @Mazim i want to do this using intentService.Is there any logic to do that ? – Numan Ahmad Sep 17 '13 at 11:16
  • AsynchTask is a huge hassle/time/efficiency/sanity saver for these tasks. Let it run on its own thread asynchronously and let the OS handle it. Slightly OT: Brogress bar sounds like a cool macho thing to have! – RossC Sep 17 '13 at 13:37
  • @NumanAhmad actually you can, but there is no much sence in that. Just use yet another BroadCast to send intents like 'progress updated' and catch them in your Activity with update of some progress bar. But I strongly recommend you not to do it in such way. – Maxim Efimov Sep 18 '13 at 02:19