【问题标题】:Progress Download Dialog进度下载对话框
【发布时间】:2012-08-17 16:35:59
【问题描述】:

如何根据以下代码实现进度下载对话框?我的代码从特定 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();  
        }
        }

我的尝试:捕获异常。请参考以下代码

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

【问题讨论】:

标签: android dialog progress


【解决方案1】:

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

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

更多信息请查看LINK

【讨论】:

    【解决方案2】:

    试试这个

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

    在onOptionsItemSelected(MenuItem item)中

    接下来使用Handlers来处理http://developer.android.com/reference/android/os/Handler.html

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多