-2

I have a gallery I have some pictures in my gallery that there are in the drawable. like this first I put the into in Integer Array like this.

Integer stickers_big[] = { R.drawable.stck1, R.drawable.stck2,...}

and this my adapter :

    class stickersAdapter extends BaseAdapter {
    Context ctx;

    public stickersAdapter(FirstActivity act) {
        // TODO Auto-generated constructor stub
        this.ctx = act;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return stickers_drawable.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ImageView imageview = new ImageView(ctx);
        imageview.setImageResource(stickers_drawable[position].intValue());
        imageview.setScaleType(android.widget.ImageView.ScaleType.FIT_XY);

        return imageview;
    }

}

Now I want to put my images into assets . I do like this , I read my image name and then set it into my imagview. but I JUST read one image name. I dont know how to read the name of all my images and open them.

 public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ImageView imageview = new ImageView(ctx);
        AssetManager assetManager = getAssets();
        try {
            // get input stream
            InputStream ims = assetManager.open("stck1.png");

            // create drawable from stream
            Drawable d = Drawable.createFromStream(ims, null);

            // set the drawable to imageview
            imageview.setImageDrawable(d);
        }
        catch(IOException ex) {

        }


        return imageview;
    }

}

How to read ALL of my images then open them?
Suraj Makhija
  • 1,368
  • 6
  • 16
nsr
  • 95
  • 10

3 Answers3

1

Now I want to put my images into assets .

At first keep your images into image folder and then use this way

 int imageResource = ctx.getResources().getIdentifier("@drawable/stck1".replace(".jpg", ""), null,ctx.getPackageName());
 imageview.setImageResource(imageResource);
IntelliJ Amiya
  • 70,230
  • 14
  • 154
  • 181
1

create sub-folder in assets directory. use getAssets().list() for getting all file names from assets:

 String[] images =getAssets().list("images");
ArrayList<String> listImages = new ArrayList<String>(Arrays.asList(images));

Now to set image in imageview you first need to get bitmap using image name from assets :

InputStream inputstream=mContext.getAssets().open("images/"
                                      +listImages.get(position));
Drawable drawable = Drawable.createFromStream(inputstream, null);
imageView.setImageDrawable(drawable);
Sandeep dhiman
  • 1,637
  • 2
  • 13
  • 22
0

Say you have this hierarchy in your assets folder

assets
 |__img1
 |__img2
 |__img3 

you can get them like this

try {  
    String[] list = getAssets().list("");

    for(String img: list ){
        //do your stuff here
       }
    } catch (IOException e) {
             e.printStackTrace();
     }

if you have sub-folder in assets, put its name in list("sub_folder");

You can get this list in your activity, if you are in fragment you will have to do getActivity()getAssets().list("");. Once you have list of your images name, you may pass it to you adapter and can use it in your getView() method w.r.t position.

Junaid Hafeez
  • 1,558
  • 1
  • 15
  • 23
  • where you want to get all the names which you have put in your assets. `list` will have all those names and you can loop them to get every single name. – Junaid Hafeez Apr 18 '17 at 07:30
  • Would you please take a look at Sandeep dhiman answer , Now in this way I can get image names easliy but my gallery dont show my picture – nsr Apr 18 '17 at 07:54