Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Bitmap to drawable and store it in array

Tags:

android

image

i am using image slider in my layout, for this i used

      private Integer[] mImageIds = { R.drawable.j, R.drawable.f,R.drawable.i,    R.drawable.g, R.drawable.k }; 

to take the images.But now i want to take the images from sdcard and store these images into that array.How can i do this? below is my code. i stored thumb in database.I converted bitmap to drawable and then how to store this into Array mImages?

      List<String> thumb = new ArrayList<String>();
     public Integer[] mImageIds;
     Bitmap bitmap;
            thumb = db.getRecomdThumb();//get image address from db
    for(int i = 0; i < thumb.size();i++){
        bitmap = BitmapFactory.decodeFile(thumb.get(i));
        Drawable d = new BitmapDrawable(getResources(),bitmap);

    } 
like image 206
Brinda K Avatar asked Aug 10 '26 20:08

Brinda K


1 Answers

Only the images you store inside the "res" folder can be added to a Integer array. You can create a int res from dynamically created Bitmap. You can only convert it to Drawable as you have mentioned in your code.

If you ask me, will it be possible to get Int representation if I add the dynamically created drawable to res folder, it is not possible.

"Res" folder can't be modified. It is read only.

So what you have to do is, just create a Drawable array instead of Integer array and do your operation.

Something like this would work,

  List<String> thumb = new ArrayList<String>();
     public Integer[] mImageIds;
     Bitmap bitmap;
            thumb = db.getRecomdThumb();//get image address from db
  private Drawable[] drawables = new Drawable[thumb.size()];
    for(int i = 0; i < thumb.size();i++){
        bitmap = BitmapFactory.decodeFile(thumb.get(i));
        drawables[i] = new BitmapDrawable(getResources(),bitmap);

    } 
like image 95
Andro Selva Avatar answered Aug 12 '26 10:08

Andro Selva



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!