2

I have a RecyclerView in which I am displaying an integer array of Drawable. However I have a folder of Bitmaps on the sd card I want to display instead. But I have not been able to work out how to convert the bitmaps into drawables and put them into an int array for display or even if this is the best way of working this out.

This is this is the code for where I set my adapter in onCreateView where I use getData() to give the images for the adapter:

adapter = new ProgressImagesListAdapter(getActivity(), getData());
        mProgressImagesListRecycler.setAdapter(adapter);

This is getData():

public List<ProgressImagesListInformation> getData() {

    List<ProgressImagesListInformation> mFrontViewImagesList = new ArrayList<>();

    int[] mImages = new int[]{
            R.drawable.boomer,
            R.drawable.bosco,
            R.drawable.brewster,
            R.drawable.dory,
            R.drawable.gabby,
            R.drawable.hunter,
            R.drawable.kitkat,
            R.drawable.doug,
            R.drawable.mae,
            R.drawable.millie
    };

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

    {
        ProgressImagesListInformation current = new ProgressImagesListInformation();
        current.mImageId = mImages[i];
        mFrontViewImagesList.add(current);
    }

    return mFrontViewImagesList;
}

The way I was thinking was to change the contents of getdata(), make a for loop that converts the contents of sd card folder bitmaps into array of drawables using somthing like the line below in the for loop but I have not been able to work a way to implement it. This obviously gives me the error found type bitmap expected type integer but I don't understand why or how to fix it.

ArrayList<Integer> ImagesArray = new ArrayList<>();

for (int i = 0; i < sNumberOfFilesInImageDirectory; i++)

{
    ImagesArray.add(new BitmapDrawable(getResources(), MyBitmapImage));
}

Thanks for your help.

Mauker
  • 11,237
  • 7
  • 58
  • 76
Nicholas Muir
  • 2,897
  • 8
  • 39
  • 89
  • So tell me if I'm right, You want a RecyclerView that shows a list of images? – wnieves19 Jan 05 '16 at 03:01
  • @snowman28924 yes you are right, but it is a list of images in a folder on an SD card. – Nicholas Muir Jan 05 '16 at 03:05
  • Ok, so the initial step would be to get those images from the SD card and coverting them into bitmaps, second, you should make a RecyclerView that display a list of CardViews which in turn contains an imageView inside, on that image view you will put each one of your bitmaps. Here's the android documentation for that http://developer.android.com/training/material/lists-cards.html. There's also plenty of examples that do just that Good luck – wnieves19 Jan 05 '16 at 03:14
  • Thanks @snowman28924 but that is not what I was asking. I already have the recyclerview displaying images from drawable. I was trying to change what was displaying not redo the recyclerview. – Nicholas Muir Jan 05 '16 at 03:49

1 Answers1

3

You'll have to know the location of those images on the SD card. Are they always on the same folder? If so, it'll be easier for you.

First of all, don't forget to declare android.permission.READ_EXTERNAL_STORAGEon your AndroidManifest.xml file.

Then, you'll need to use BitmapFactory along with the path to your images to get the Bitmap objects and put them into a List like an ArrayList or simply use an array.

After that you can use the images like any other. But they won't be int resources, they'll be actual Bitmap objects. To use the resources like R.drawable.someimage they'll have to be located under your res/ folder.

Example:

BitmapFactory.Options opt= new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, opt);
list.add(bitmap); // Or do something else with the Bitmaps.

And if you want to save those images elsewhere, check this answer.

The other steps would require you to understand how to use CardView and RecyclerView. You can read this post on Android Essence and this one on Big Nerd Ranch, and of course this one from the android training guide to get going.

**EDIT: ** And to use a Bitmap on an ImageView, use this:

Bitmap yourBitmap; // Getting from somewhere else.
ImageView myImage = (ImageView) findViewById(R.id.myimageview);
myImage.setImageBitmap(yourBitmap);

And if for some reason you have a Drawable object instead of a Bitmap you can call:

Drawable yourDrawable; // Getting from somewhere else.
ImageView myImage = (ImageView) findViewById(R.id.myimageview);
myImage.setImageDrawable(yourDrawable);

More info on ImageView on this link.

Community
  • 1
  • 1
Mauker
  • 11,237
  • 7
  • 58
  • 76
  • Thanks for the response, – Nicholas Muir Jan 05 '16 at 03:51
  • I have implemented your method for creating the arraylist of bitmap objects luckily I am creating the images and I can iterate the names. currently I am passing the int array of resource ids to my onbindviewholder like this holder.FrontView.setImageResource(current.mImageId). Will set image resource work if I pass in the bitmap objects I just created with your method? – Nicholas Muir Jan 05 '16 at 04:13
  • No, it won't. You'll have to call [`ImageView#setImageBitmap()`](http://developer.android.com/reference/android/widget/ImageView.html#setImageBitmap(android.graphics.Bitmap)) instead. – Mauker Jan 05 '16 at 04:20
  • that worked perfectly, the recyclerview is now showing all my images, thanks! Now I just need to work out why it is shrinking the images. – Nicholas Muir Jan 05 '16 at 04:39
  • You're welcome again :) About your last question, take a look [at this](http://developer.android.com/reference/android/widget/ImageView.ScaleType.html) And don't forget to mark as accepted if it helped ;) – Mauker Jan 05 '16 at 04:51