How can i get images dynamically from drawable folder?

user9982192

I'm getting images by statically given the name like

{R.drawable.image1,R.drawable.image2,R.drawable.image3}

If I have some 50 images I cant give each and every file name in array so it needs to be dynamic how can I achieve this. And also please tell me where to put my dynamically code

package com.example.nauman.swipeapp;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class SwipeAdapter extends PagerAdapter {
    private int[]image =  {R.drawable.image1,R.drawable.image2,R.drawable.image3};

    private Context cx;

    SwipeAdapter(Context cx){

        this.cx=cx;
    }

    @Override
    public int getCount() {
        return image.length;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return (view==(RelativeLayout)object);
    }

    @SuppressLint("SetTextI18n")
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {

        LayoutInflater layoutInflater = (LayoutInflater) cx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        assert layoutInflater != null;
        View view= layoutInflater.inflate(R.layout.page_fragment,container,false);
        ImageView imageView= view.findViewById(R.id.imageView);
        imageView.setImageResource(image[position]);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((RelativeLayout) object);
    }

}
Bö macht Blau

This solution feels a little bit hacky not only because it uses reflection but also because it relies heavily on being able to recognize your own drawable resources by matching their names against a certain pattern (so you should try to use really unique names for the pictures)

That being said, you can change your code as follows:

Keep the private int[] image; but initialize it in the Adapter's constructor:

SwipeAdapter(Context cx){
    this.cx=cx;
    buildImageArray();
}

with a new method (note: you have to import java.lang.reflect.Field;)

private void buildImageArray() {

    // this will give you **all** drawables, including those from e.g. the support libraries!
    Field[] drawables = R.drawable.class.getDeclaredFields();
    SparseIntArray temp = new SparseIntArray();
    int index = 0;
    for (Field f : drawables) {
        try {
            System.out.println("R.drawable." + f.getName());
            // check the drawable is "yours" by comparing the name to your name pattern
            // this is the point where some unwanted drawable may slip in,
            // so you should spend some effort on the naming/ matching of your pictures
            if (f.getName().startsWith("image")) {
                System.out.println("R.drawable." + f.getName() + "==========================================");
                int id = cx.getResources().getIdentifier(f.getName(), "drawable", cx.getPackageName());
                temp.append(index, id);
                index++;
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    image = new int[index];
    for (int i = 0; i < index; i++) {
        image[i] = temp.valueAt(i);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I dynamically render images from my images folder using Jinja and Flask?

How to get all images as array of bitmap from drawable folder using Drawable?

How do I pass a R.drawable as a parameter so I can get images parsed

How can I access the images folder from code behind

How can I display images from a folder in random order with javascript?

How can I import images from another folder?

How do i get images file name from a given folder

How can I get drawable in Kotlin?

How can I get drawable resource by string?

I can't insert icons from the drawable folder

Could not select images from drawable folder

If I create a link to a folder, how can I get from that linked folder to the "real" folder from within Nautilus?

how drawable folder to use to fit background images

How can I dynamically add images to a GridView?

How can subplot images from each folder?

How to get images from assets folder?

How can I get the last folder from a path string?

How can I get a folder from remote machine to local machine?

How can I get access to user folder from another computer?

how can i get value from dynamically generated edittexts?

how to store a images and retrieve from sqlite database using bitmap or from drawable folder

How can i get an image from drawable using image name stored in SQLite?

How can I get normalized coordinated from an object in images

How can I get images in react from nodeJS API server

How can I get some images with flow from directory?

How to save image path/image to database where i have my images in my drawable folder?

How can I get a function to return all the images within a folder for other functions to use with IronPython?

Get sequential file name from drawable folder

How can I dynamically load an image from my assets folder based on props in ReactJS?