下载指示

时间:2012-02-29 07:55:14

标签: android android-progressbar

大家好我有一个应用程序,它点击一个按钮存储一些文件到SD卡,但我想显示其下载或下载的指示。

如何实现这一点我可以使用进度条。

这是我的按钮代码

case R.id.dd:
File sdcard = Environment.getExternalStorageDirectory();
            File dir = new File (sdcard.getAbsolutePath() + "/varun");
            dir.mkdirs();
            File file = new File(dir, "" +var+ ".mp3");

        //  File file = new File(Environment.getExternalStorageDirectory(), "" +ver+ ".mp3");
            FileOutputStream fos;

            try {
                fos = new FileOutputStream(file);
                fos.write(bitmapdata);
                fos.flush();
                fos.close();
            } catch (FileNotFoundException e) {
                // handle exception
            } catch (IOException e) {
                // handle exception
            }

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:0)

你可以使用异步任务或线程类。使用线程你可以使用下面的代码

ProgressBar prg;
    private void start_thread(final int offer_id)
    {
     prg=ProgressDialog.show(this, null, "Getting data...",false,false);
         new Thread(new Runnable() 
         { public void run() 
         { 
            try {
                start_prc();
                mHandlerSuccess.post(mUpdateSuccess);
            } catch (Exception e) {

                mHandlerFail.post(mUpdateFail);
            }   
        }
      } ).start();
    }
    final Handler mHandlerSuccess= new Handler();

    final Runnable mUpdateSuccess = new Runnable() {
        public void run() {
            prg.hide();
            Toast.makeText(Add_remove_btnsActivity.this, "finished", Toast.LENGTH_LONG).show();

        }
    };
    final Handler mHandlerFail= new Handler();

    final Runnable mUpdateFail = new Runnable() {
        public void run() {
            prg.hide();
            Toast.makeText(Add_remove_btnsActivity.this, "failed", Toast.LENGTH_LONG).show();

        }
    };
    private void start_prc()
    {
        File dir = new File (sdcard.getAbsolutePath() + "/varun");
        dir.mkdirs();
        File file = new File(dir, "" +var+ ".mp3");

    //  File file = new File(Environment.getExternalStorageDirectory(), "" +ver+ ".mp3");
        FileOutputStream fos;

        try {
            fos = new FileOutputStream(file);
            fos.write(bitmapdata);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            // handle exception
            mHandlerFail.post(mUpdateFail);
        } catch (IOException e) {
            // handle exception
            mHandlerFail.post(mUpdateFail);
        }

    }