質問

Currently, i have a class to download an image extending of Asynctask. I'd like to implement a small progress bar to show at user an image is loaded in that space. I tried to put the Asynctask class within a class activity. The problem is now to call the class I need the class to be static. But if I put the static class I can not control the progress dialog.

My class DownloadImage:

public class DownloadImages extends Activity{

    public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;
//      progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleSmall);

        public DownloadImage(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;

            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }
        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
            Log.e("TAG","IMAGEN CARGADA");
        }

    //    protected void onPreExecute(){
    //    }
    }
}

To create the ProgressBar, I think I need the class to be an Activity:

progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleSmall);

This is the solution I created to my problem:

public class DownloadImages extends Activity{

    ProgressBar progressBar = new ProgressBar(DownloadImages.this, null, android.R.attr.progressBarStyleSmall);

    public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImage(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;

            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }
        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
            Log.e("TAG","IMAGEN CARGADA");
        }

        protected void onPreExecute(){
            progressBar.setVisibility(View.VISIBLE);
        }
    }
}

The problem now is if i call DownloadImage, this class has to be static, but if I do the static I can not use the progressBar, unless it is declared as static, and if I think static I can not create it with my Activity.

Any solution?

Thanks, and sorry for my bad English

役に立ちましたか?

解決

Use WeakReference do get the Context of the Activity from which you are calling the DownloadImage class, ProgressBar requires the Context in its constructors 1st parameter.

public class DownloadImages extends Activity{

ProgressBar progressBar = new ProgressBar(DownloadImages.this, null, android.R.attr.progressBarStyleSmall);

public static class DownloadImage extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;
    private WeakReference<DownloadImages> mWeakReference;

    public DownloadImage(DownloadImages downloadImages, ImageView bmImage) {
        this.bmImage = bmImage;
        mWeakReference= new WeakReference<DownloadImages>(downloadImages);
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;

        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }
    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
        Log.e("TAG","IMAGEN CARGADA");
    }

    protected void onPreExecute(){
        mWeakReference.get().progressBar.setVisibility(View.VISIBLE);
    }
}

}

Now pass 'this' from Activity when you start the AsyncTask.

他のヒント

Just pass your ProgressBar instance into the DownloadImage task constructor.

Simply like this (i assume that you create your AsyncTask from activity, so this will point to it):

Progressbar progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleSmall);
DownloadImage downloadImage = new DownloadImage(someImageView, progressBar);

And change your constructor so it can get ProgressBar as a parameter.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top