2012-02-29 39 views
0

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

如何达到这个我可以使用进度条。

这是我的按钮

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 
      } 

回答

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); 
     } 

    } 
+0

谢谢你会试用 – Goofy 2012-02-29 08:16:43