进展下载对话框 [英] Progress Download Dialog

查看:127
本文介绍了进展下载对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何实现进展下载对话框基于$ C $以下CS?我的codeS下载图片来自特定的URL,然后将其保存图像文件到SD卡,并设置图像作为壁纸。我想显示一个进度下载对话框当用户触摸选项菜单按钮setwallpaper()。

 公共布尔onOptionsItemSelected(菜单项项){
        开关(item.getItemId()){
            案例R.id.set_wallpaper:
                SetWallpaper(IMAGE_URL);
                返回true;
            默认:
                返回false;
        }
    }公共无效SetWallpaper(字符串IMAGE_URL)
        {
        URL myFileUrl = NULL;
            尝试
        {
                myFileUrl =新的URL(图片网址);
        }
            赶上(MalformedURLException的E)
            {
                e.printStackTrace();
            }
            位图bmImg = NULL;
            尝试{
                HttpURLConnection的康恩=(HttpURLConnection类)myFileUrl.openConnection();
                conn.setDoInput(真);
                conn.connect();
                // INT长度= conn.getContentLength();
            InputStream为= conn.getInputStream();
            bmImg = BitmapFactory.de codeStream(是);
            }
                赶上(IOException异常E)
                {
                    e.printStackTrace();
                }
        尝试{            字符串路径= myFileUrl.getPath();
            字符串IDSTR = path.substring(path.lastIndexOf('/')+ 1);
            文件的文件路径= Environment.getExternalStorageDirectory();
            文件DIR =新的文件(filepath.getAbsolutePath()+/墙纸/);
                dir.mkdirs();
                字符串文件名= IDSTR;
                档案文件=新的文件(目录,文件名);
                FOS的FileOutputStream =新的FileOutputStream(文件);                bmImg.com preSS(比较pressFormat.JPEG,75,FOS);
                fos.flush();
                fos.close();                WallpaperManager WPM = WallpaperManager.getInstance(getBaseContext());
                wpm.setBitmap(bmImg);        }
        赶上(例外五){
            e.printStackTrace();
        }
        }

我的尝试:捕获的异常。请参考下codeS

 公共类SingleMenuItemActivity延伸活动{    // XML节点键
静态最后弦乐KEY_TITLE =称号;
静态最后弦乐KEY_ARTIST =艺术家;
静态最后弦乐KEY_THUMB_URL =thumb_url;
进度进度;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.single_list_item);        ImageView的图像=(ImageView的)findViewById(R.id.single_image);
        在意向= getIntent();        字符串IMAGE_URL = in.getStringExtra(KEY_THUMB_URL);
        ImageLoader的imgLoader =新ImageLoader的(getApplicationContext());
        imgLoader.DisplayImage(IMAGE_URL,图像);        字符串title = in.getStringExtra(KEY_TITLE);
        串艺术家= in.getStringExtra(KEY_ARTIST);
        TextView的lblName =(的TextView)findViewById(R.id.name_title);
        TextView的lblCost =(的TextView)findViewById(R.id.name_artist);
        进度=(进度)findViewById(R.id.loadingBar);        lblName.setText(职称);
        lblCost.setText(艺术家);
    }    公共类loadImageTask扩展的AsyncTask<弦乐,太虚,太虚>
    {
        //可绘制imgLoad;
        URL myFileUrl = NULL;
        位图bmImg = NULL;
        @覆盖
        在preExecute保护无效(){
            // TODO自动生成方法存根
            在意向= getIntent();
        字符串IMAGE_URL = in.getStringExtra(KEY_THUMB_URL);
        尝试{
            myFileUrl =新的URL(图片网址);
        }赶上(MalformedURLException的E){
            // TODO自动生成catch块
            e.printStackTrace();
        }
        super.on preExecute();        progressbar.setVisibility(View.VISIBLE);
    }        @覆盖
        保护无效doInBackground(字符串... PARAMS){
            // TODO自动生成方法存根            尝试{
                HttpURLConnection的康恩=(HttpURLConnection类)myFileUrl.openConnection();
                conn.setDoInput(真);
                conn.connect();
                InputStream为= conn.getInputStream();
                bmImg = BitmapFactory.de codeStream(是);
            }
            赶上(IOException异常E)
            {
                e.printStackTrace();
            }
            尝试{                字符串路径= myFileUrl.getPath();
                字符串IDSTR = path.substring(path.lastIndexOf('/')+ 1);
            文件的文件路径= Environment.getExternalStorageDirectory();
            文件DIR =新的文件(filepath.getAbsolutePath()+/墙纸/);
                dir.mkdirs();
                字符串文件名= IDSTR;
                档案文件=新的文件(目录,文件名);
                FOS的FileOutputStream =新的FileOutputStream(文件);
                bmImg.com preSS(比较pressFormat.JPEG,75,FOS);
                fos.flush();
                fos.close();
            }
            赶上(例外五)
                    {
                        e.printStackTrace();
                    }
            返回null;
        }
        @覆盖
        保护无效onPostExecute(虚空结果){
            // TODO自动生成方法存根
            super.onPostExecute(结果);            如果(progressbar.isShown())
            {
                progressbar.setVisibility(View.GONE);            }
        }
    }
    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        。getMenuInflater()膨胀(R.menu.main_menu,菜单);
        返回true;
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        在意向= getIntent();
        字符串IMAGE_URL = in.getStringExtra(KEY_THUMB_URL);
        开关(item.getItemId()){
            案例R.id.save_image:
                新loadImageTask()执行(IMAGE_URL);                返回true;
            默认:
                返回false;
        }


解决方案

在这里,您可以使用AsyncTask的为展会的进度/ DialogBox的。

显示进度条上的preExecute()方法中可见。并执行doInBackground()方法中的图像加载操作。一旦这个操作完成后,我们将进度条无形,使ImageView的可见与onPostExecute()方法中的加载图像。

有关详细信息,请检查该链接

How do you implement Progress Download Dialog based on the codes below? My codes downloads images from a specific URL then it saves the image file into SD Card and set the image as wallpaper. I want to show a Progress Download Dialog when the user touches the options menu button 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();  
        }
        }

My attempt : Caught Exception . Please refer below codes

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

解决方案

Here you can use the AsyncTask for the show the progressbar/dialogbox .

Show the progress bar visible inside the onPreExecute() method. And performs the image loading operations inside the doInBackground() method. Once this operation is done, we will make progress bar invisible and make imageview visible with that loaded image inside the onPostExecute() method.

For more information check this LINK

这篇关于进展下载对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆