Reputation: 3114
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.
Upvotes: 2
Views: 888
Reputation: 11497
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_STORAGE
on 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.
Upvotes: 3