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);
Related
How to get ImageView src programmatically and set it in another ImageView
Drawable drawable = imageViewA....?
imageViewB.setImageDrawable(drawable);
You can do something like this:
Drawable drawable = imageViewA.getDrawable();
if(drawable != null){
imageViewB.setImageDrawable(drawable);
}
You can use setImageResource(int)
imageView.setImageResource(R.drawable.bg_image);
Instead of get drawable you do...
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
and set to new image
newImageView.setImageBitmap(bitmap);
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.
I have some buttons which i set the background with setBackgroundResource(R.drawable.cal_box_center); and the problem I'm having is that my background is a gradient which has this banding effect (annoying) and I've read that in order to remove this you'll need to set the Bitmap.Config.ARGB_8888. I looked into the API and the way to do this is to use decodeStream etc, but how can i use setBackgroundResource and still set the Config to ARGB_8888?
Thanks in advance.
You can use this code snippet:
// create button
Button btn = new Button(getApplicationContext());
//decode the resource(You can also use decodeStream and other decode method of
//BitmapFactory)
Bitmap btm = BitmapFactory.decodeResource(getResources(), R.drawable.cal_box_center);
//create another copy of your bitmap and specify Config
Bitmap newBtm = btm.copy(Bitmap.Config.ARGB_8888, true);
//use your newBtm to create a BitmapDrawable
BitmapDrawable btmDrwble = new BitmapDrawable(newBtm);
// finally set the drawable as your button's background
btn.setBackgroundDrawable(btmDrwble);
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]);
I am making an android app in which i need to enlarge the image size on click.
I am getting these images downloaded from server and then adding these images programmatically in the activity and hence setting id of images programmatically. Now my problem is that when i send the imageid o another activity to enlarge it it is taking only the id of last image.
and hence gives the following exception.
Unable to start activity ComponentInfo{com.emx.OnDaMove/com.emx.OnDaMove.FullImageActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x5
i am using the following code for making images and adding them to table rows.
imageUrl = "http://ondamove.it/English/images/users/";
imageUrl = imageUrl + listObject.get(j).getImage();
image = new ImageView(this);
image.setPadding(20, 10, 0, 0);
image.setId(j+1);
imageId=image.getId();
tr.addView(image);
and the following method for clickliastener:
private OnClickListener clickImageListener = new OnClickListener() {
public void onClick(View v) {
Intent fullScreenIntent = new Intent(v.getContext(),FullImageActivity.class);
fullScreenIntent.putExtra(ProfilePageNormalUser.class.getName(),imageId) ;
ProfilePageNormalUser.this.startActivity(fullScreenIntent);
}
};
and the activity in which images are to be enlarged is:
ImageView myimage = (ImageView) findViewById(R.id.imageviewEnlarged);
Intent intent = getIntent();
int imageId = (Integer) intent.getExtras().get(ProfilePageNormalUser.class.getName());
System.out.println("********"+imageId +"**********");
InputStream is = this.getResources().openRawResource(imageId);
Bitmap originalBitmap = BitmapFactory.decodeStream(is);
Matrix imageMatrix = new Matrix();
imageMatrix.postRotate(90);
Bitmap scaledBitmap = Bitmap.createBitmap(originalBitmap, myimage.getWidth(), myimage.getHeight(), originalBitmap.getWidth(), originalBitmap.getHeight(), imageMatrix, false);
myimage.setImageBitmap(scaledBitmap);
myimage.setScaleType(ScaleType.FIT_XY);
Ya surely it will give the last Id since the value of imageId will be update on every loop. This will store the last known value during looping.
You should use array instead of simple variable.
or you can use v.getId in onClick method. (simpler one)