Convert Bitmap to drawable and store it in array - android

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);
}

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);
}

You could just create a
private Drawable[] drawables = new Drawable[mImageIds.length];
and copy the images at the respective size.
Having said that: drawables may be large and storing them (or the bitmaps) in memory may make you quickly run into out-of-memory situations as the handsets usually have small heap sizes.

Related

android change background of activity

I am trying to use the following to change the background to an image stored on the sd card:
String pathName = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ "/newbackground.jpg";
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeFile(pathName);
BitmapDrawable bd = new BitmapDrawable(res, bitmap);
View view = findViewById(R.id.fred);
view.setBackgroundDrawable(bd);
But it keeps crashing.
I´m using setBackgroundDrawable as I want it to work below api 16 and it is deprecated.
I put in a check to see if pathname exists and it does.
Any ideas?
mark

Set ImageView Drawable programmatically

I am trying to set the image of a ImageView programmatically from a array of drawables but keep getting a NUllPointerException....does this method look correct for setting Image resource..
//get a random drawable
int[] imageSelection= {R.drawable.buddycheck, R.drawable.logdive3, R.drawable.sea, R.drawable.weather, R.drawable.logo1};
Random whichImage = new Random();
int theImage = whichImage.nextInt(imageSelection.length);
displayImage.setBackgroundResource(theImage);
You are setting the image resource to a random number.
You need to do it like this:
int theImage = imageSelection[whichImage.nextInt(imageSelection.length)];
displayImage.setBackgroundResource(theImage);
Try this : imageView.setImageResource(R.drawable.image);

Linearlayout background image in assets folder

I want to use a Image from the assets folder as my background image in android
but the only picture I can use is out oif the drawable folder
random = String.valueOf(util_random.random(1, 4));
String drawable = "/assets/images/img"+random ;
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundDrawable(drawable);
this.setContentView(ll);
You cannot pass a String in View.setBackgroundDrawable(Drawable).
Use BitmapFactory to create a Bitmap, then create a BitmapDrawable and use it as your background image.

Get the image associated with an ImageView

I like to know whether there is any way by which we could get to know which image is associated with a particular imageview.
ImageView iv = new ImageView(r.drawable.pic);
If this was like this i could have know that imageview iv conatins "pic". But this is not the case in my app.
Recently i have been developing an app in which i have 26 imageview,and i use to set the images associated which each of these imageviews randomly. That is, I couldnt know predict which imageview will contain which image.
But i need to find out which image is actually associated which each imageview. I also have 26 images, one for each imageview.
How about when you set the tag to something you know:
ImageView image = (ImageView)whatever;
image.setTag("key", "Image ID");
then
String imageId = (String)imageView.getTag("key");
ImageView iv12=(ImageView)findViewById(R.id.imageView2);
ImageView iv13=(ImageView)findViewById(R.id.imageView3);
// ..............iv36.......
Random ran=new Random();
for (int i = image.length - 1; i >= 0; i--) {
int index = ran.nextInt(i + 1); // Simple swap
int a = image[index];
image[index] = image[i];
image[i] = a;
}
//..........................
final Drawable d1 = getResources().getDrawable(image[0]);
final Drawable d2 = getResources().getDrawable(image[1]);
ok
// Then I assume
iv1.setImageResource(d1); // ?
iv1.setTag("key", image[0]);

Displaying random images

im novice to android.For displaying random images i used arraylist.
ArrayList<Integer> List = new ArrayList<Integer>();
for(int i=0;i<10;i++)
{
List.add(i);
System.out.println("Random Images"+List.add(i));
}
Collections.shuffle(List);
for(int i=0;i<10;i++){
imageArr[i] = (ImageView)findViewById(id[List.get(i)]);
System.out.println("Display Images"+List.get(i));
}
}
it is running correctly in logcat but what should do to display images on emulator screen. Pls Suggest
You will need an ImageView to display Images on the Screen.
You can display drawable, bitmaps, ...
The easiest way to do this create your main.xml like this:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/myImageView" />
Then call in your code (maybe in the onCreate):
ImageView imgView = (ImageView) findViewById(R.id.myImageView);
Then make a Drawable Array or an ArryList with bitmaps, whatever.
Get a Random value (like Math.random()) and fetch a random image from array or arraylist like
Drawable drawable = drawableArray[YOURRANDOMNUMBER];
and set the Drawable to the imageview.
imgView .setImageDrawable(drawable);
Hope this helps :)
Give this a read and see if it helps you: http://www.higherpass.com/Android/Tutorials/Working-With-Images-In-Android/

Categories

Resources