2012-08-17 96 views
1

如何根据下面的代码实现进度下载对话框?我的代码从特定的URL下载图像,然后将图像文件保存到SD卡中,并将图像设置为壁纸。我想在用户触摸选项菜单按钮setwallpaper()时显示“进度下载”对话框。进度下载对话框

public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.set_wallpaper: 
       SetWallpaper(image_url); 
       return true; 
      default: 
       return false; 
     } 
    } 



public void SetWallpaper(String image_url) 
     { 
     URL myFileUrl = null; 
      try 
     { 
       myFileUrl = new URL(image_url); 
     } 
      catch (MalformedURLException e) 
      {  
       e.printStackTrace(); 
      } 
      Bitmap bmImg = null; 
      try { 
       HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection(); 
       conn.setDoInput(true); 
       conn.connect();  
       //int length = conn.getContentLength(); 
      InputStream is = conn.getInputStream(); 
      bmImg = BitmapFactory.decodeStream(is); 
      } 
       catch (IOException e) 
       {  
        e.printStackTrace(); 
       } 
     try {  

      String path = myFileUrl.getPath(); 
      String idStr = path.substring(path.lastIndexOf('/') + 1); 
      File filepath = Environment.getExternalStorageDirectory(); 
      File dir = new File (filepath.getAbsolutePath() + "/Wallpaper/"); 
       dir.mkdirs(); 
       String fileName = idStr; 
       File file = new File(dir, fileName); 
       FileOutputStream fos = new FileOutputStream(file); 

       bmImg.compress(CompressFormat.JPEG, 75, fos); 
       fos.flush();  
       fos.close();  

       WallpaperManager wpm = WallpaperManager.getInstance(getBaseContext()); 
       wpm.setBitmap(bmImg); 

     } 
     catch (Exception e){ 


      e.printStackTrace(); 
     } 
     } 

我的企图:被捕的异常。在onOptionsItemSelected(菜单项项)

,并在下一次使用处理程序使用此

final ProgressDialog dialog = ProgressDialog.show(
      YourACtivity.this, "", "Loading...please wait"); 

    new Thread(new Runnable() { 
     @Override 
     public void run() { 
     SetWallpaper(image_url); 
        if (dialog != null && dialog.isShowing()) { 
         dialog.dismiss(); 
        } 
     } 
    }).start(); 

} 

代替

SetWallpaper(image_url); 

处理请参考下面的代码

public class SingleMenuItemActivity extends Activity { 

    // XML node keys 
static final String KEY_TITLE = "title"; 
static final String KEY_ARTIST = "artist"; 
static final String KEY_THUMB_URL = "thumb_url"; 
ProgressBar progressbar; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.single_list_item); 

     ImageView image = (ImageView) findViewById(R.id.single_image); 
     Intent in = getIntent(); 

     String image_url = in.getStringExtra(KEY_THUMB_URL); 


     ImageLoader imgLoader = new ImageLoader(getApplicationContext()); 


     imgLoader.DisplayImage(image_url, image); 

     String title = in.getStringExtra(KEY_TITLE); 
     String artist = in.getStringExtra(KEY_ARTIST); 


     TextView lblName = (TextView) findViewById(R.id.name_title); 
     TextView lblCost = (TextView) findViewById(R.id.name_artist); 
     progressbar = (ProgressBar) findViewById(R.id.loadingBar); 

     lblName.setText(title); 
     lblCost.setText(artist); 


    } 

    public class loadImageTask extends AsyncTask<String, Void, Void> 
    { 
     //Drawable imgLoad; 
     URL myFileUrl = null; 
     Bitmap bmImg = null; 
     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      Intent in = getIntent(); 
     String image_url = in.getStringExtra(KEY_THUMB_URL); 
     try { 
      myFileUrl = new URL(image_url); 
     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     super.onPreExecute(); 

     progressbar.setVisibility(View.VISIBLE); 
    } 

     @Override 
     protected Void doInBackground(String... params) { 
      // TODO Auto-generated method stub 

      try { 
       HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection(); 
       conn.setDoInput(true); 
       conn.connect();  
       InputStream is = conn.getInputStream(); 
       bmImg = BitmapFactory.decodeStream(is); 
      } 
      catch (IOException e) 
      {  
       e.printStackTrace(); 
      } 
      try {  

       String path = myFileUrl.getPath(); 
       String idStr = path.substring(path.lastIndexOf('/') + 1); 
      File filepath = Environment.getExternalStorageDirectory(); 
      File dir = new File (filepath.getAbsolutePath() + "/Wallpaper/"); 
       dir.mkdirs(); 
       String fileName = idStr; 
       File file = new File(dir, fileName); 
       FileOutputStream fos = new FileOutputStream(file); 
       bmImg.compress(CompressFormat.JPEG, 75, fos); 
       fos.flush();  
       fos.close();  
      } 
      catch (Exception e) 
        { 
         e.printStackTrace(); 
        } 
      return null; 
     } 
     @Override 
     protected void onPostExecute(Void result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 

      if(progressbar.isShown()) 
      { 
       progressbar.setVisibility(View.GONE); 

      } 
     } 
    } 




    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.main_menu, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     Intent in = getIntent(); 
     String image_url = in.getStringExtra(KEY_THUMB_URL); 
     switch (item.getItemId()) { 
      case R.id.save_image: 
       new loadImageTask().execute(image_url); 

       return true; 
      default: 
       return false; 
     } 
+0

进度条或动画微调? – 2012-08-17 03:21:35

+2

为此使用'Async Task'。 http://www.vogella.com/articles/AndroidPerformance/article.html#asynctask – 2012-08-17 03:46:39

+0

我需要一个进度对话框。所以极客 – 2012-08-17 03:49:20

回答

1

在这里你可以使用AsyncTask来显示进度条/对话框。

显示onPreExecute()方法内可见的进度条。并在doInBackground()方法内执行图像加载操作。完成此操作后,我们将使进度条不可见,并使用onPostExecute()方法内的加载图像使imageview可见。

欲了解更多信息,查看此LINK

0
+0

你可以关闭后台线程中的'dialog'吗? – 2012-08-17 03:47:12

+0

Mohsin提到我的setwallpaper需要异步任务吗? – 2012-08-17 03:50:58

+0

是的你甚至可以使用异步任务 检查这个http://rorraja.blogspot.in/2012/03/show-progress-dialog-inside-asynctask.html – 2012-08-17 03:56:00