Michael
Michael

Reputation: 439

How to make image slider from assets images

I'm trying to make something like Image changer from the images that located in assets/sliderImages folder.

Basically , when I debugging the method, so the it list equals to the number if the items in the assets/.. folder.

But something is wrong and the Images not shown.

Here is the method to change images.

private ImageView mAuthBackGround;

public void setSlider() {
    AssetManager assetManager = getApplicationContext().getAssets();
    try {
        String[] files = assetManager.list("sliderImages");
        List<String> it = Arrays.asList(files);

        for (int i = 0; i < it.size(); i++) {
            int finalI = i;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Drawable drawable = Drawable.createFromPath(it.get(finalI));
                    mAuthBackGround.setImageDrawable(drawable);
                }
            }, 200 * i);

        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

Upvotes: 0

Views: 218

Answers (2)

Michael
Michael

Reputation: 439

Done...

public void setSlider() {
        AssetManager assetManager = getApplicationContext().getAssets();
        try {
            String[] files = assetManager.list("sliderImages");
            List<String> it = Arrays.asList(files);

            for (int i = 0; i < it.size(); i++) {
                int finalI = i;
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        imageView.setImageBitmap(getBitmapFromAssets(it.get(finalI)));
                    }
                }, 2000 * i);

            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private Bitmap getBitmapFromAssets(String fileName) {
        AssetManager assetManager = getAssets();

        InputStream istr = null;
        try {
            istr = assetManager.open("sliderImages/" + fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Bitmap bitmap = BitmapFactory.decodeStream(istr);
        try {
            istr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bitmap;
    }

Upvotes: 0

Hamza Regardless
Hamza Regardless

Reputation: 83

Brother You can Use a git Library to make Amazing slider They have all the file that you need Copy them and use in your project let me now if it helps you.Or any other help you need.

Here is the link of slider

Upvotes: 1

Related Questions