简体   繁体   中英

Downloading Image by URL using AsyncTask and using AnimationDrawable class to change images in intervals

Hi guys trying to implement AsyncTask here in my project, there seems to be no error shown by Android Studio either and have debugged to see if the bitmaps are downloaded and yes it is. I dont know what the problem is so here is my code.

My Activity:

public class MainActivity extends AppCompatActivity {

    public ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView =findViewById(R.id.justAnImage);

        String[] strings = new String[2];
        strings[0] = "blah.com/sds.jpg";
        strings[1] = "blah.com/sds2.jpg";
        new AsyncDownloader(this,imageView).execute(strings);
    }

    public void StartAnimation(ImageView imageView, Bitmap[] bitmaps)
    {
        AnimationDrawable animation = new AnimationDrawable();
        for (int i=0;i<bitmaps.length;i++) //replaced erroneous code-> (int i=0;i>=bitmaps.length;i++) 
        {
            animation.addFrame(new BitmapDrawable(getApplicationContext().getResources(),bitmaps[i]),1000);
        }
        animation.setOneShot(false);

        imageView.setBackground(animation);

        // start the animation!
        animation.start();
    }
}

My Async downloader class

public class AsyncDownloader extends AsyncTask<String[],Void,Bitmap[]> {

    private Bitmap[] bitmapArray;
    private ImageView imageView;
    MainActivity mainActivity;

    public AsyncDownloader(MainActivity mainActivity,ImageView imageView) {
        this.imageView = imageView;
        bitmapArray = new Bitmap[2];
        this.mainActivity = mainActivity;
    }

    @Override
    protected Bitmap[] doInBackground(String[]... strings) {

        for(int i=1;i>=0;i--)
        {
            try {
                URL url = new URL(strings[0][i]);
                bitmapArray[i] = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            }  catch (Exception e) {
                Log.e("DOWNLOAD ASYNC", e.getMessage());
            }
        }
        return bitmapArray;
    }

    @Override
    protected void onPostExecute(Bitmap[] bitmapArray) {
        super.onPostExecute(bitmapArray);
        //imageView.setImageBitmap(bitmapArray[0]);
        mainActivity.StartAnimation(imageView,bitmapArray);
    }
}

I cant find the problem here, debugging is so terrible in Android Studio. If any one could help, i shall be thankful.

EDIT 1: After suggestions from fellow stack guys, it is clear that the animation drawable is not working as desired and nonetheless there is no image showing up on the ImageView control.

EDIT 2: Code works after correcting the loop specified in the answer. Found an alternative to display images after certain interval. The code goes like this :

public void StartAnimation(final ImageView imageView, final Bitmap[] bitmaps)
{
    handler = new Handler();
    Runnable runnable = new Runnable() {
        int i = 0;

        public void run() {
            imageView.setImageBitmap(bitmaps[i]);
            i++;
            if (i > bitmaps.length - 1) {
                i = 0;
            }
            handler.postDelayed(this, 1200);
        }
    };
    handler.postDelayed(runnable, 1200);
}

Your code is perfect, except loop condition inside method StartAnimation .

issue is with loop condition which is always false .

To fix the issue change

From

for (int i = 0; i >= bitmaps.length; i++)

To

for (int i = 0; i < bitmaps.length; i++)

use fresco or picosso or glide library

these libraries automatically caching and managing memory.

it's easy to work with picosso and glide but fresco adds more features to developers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM