android:enlarging an image on click - android

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)

Related

How to get the drawable from relative layout in android

What i have done:
I have set the bitmap for a relative layout as below
RelativeLayout root;
root= (RelativeLayout) itemView.findViewById(R.id.root);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap preview_bitmap = BitmapFactory.decodeResource(context.getResources(),godImages[position], options);
Drawable drawable = new BitmapDrawable(context.getResources(),preview_bitmap);
root.setBackgroundDrawable(drawable);
What i am trying to do:
I am trying to get the bitmap from the relativelayout back again that was set
so that i can pass to another activity.
Note: i am aware of using the intents to padd data between activities only i want to know how to get the bitmap
Any ideas on How to achieve this ?
There are a couple of ways you can do this:
1. Try:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.rel);
layout.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(layout.getDrawingCache());
layout.setDrawingCacheEnabled(false);
2. Or try:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.rel);
Bitmap b = Bitmap.createBitmap( layout.getLayoutParams().width, layout.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
layout.layout(layout.getLeft(), layout.getTop(), layout.getRight(), layout.getBottom());
layout.draw(c);
To get the current ImageView
1. To get current position, use
ViewPager viewPager = getView().findViewById(r.id.pager);
int position = viewPager.getCurrentItem();
2. To get the current View, use
View currentView = viewPager.getChildAt(int position);
This currentView is the View created by the onCreateView() method of the current Fragment.
3. To get the ImageView, use
ImageView currentImage = (ImageView)currentView.findViewById(R.id.image);
where R.id.image is the ImageView object you want from the current Fragment.

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

android - display image from gallery

I have several images stored in content://media/external/images/media. I would like to display by using ImageView and add the image view to the table. here is my code.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 24;
final Bitmap b = BitmapFactory.decodeFile("content://media"+getFileLocation(), options);
ImageView milestoneImageView = new ImageView(this);
milestoneImageView.setImageBitmap(b);
recordRow.addView(milestoneImageView);
When I print out the getFileLocation(), it says "/external/images/media/62". So I added "content://media" to make a full path, but it is not working...
Can someone point out why the code above is not working?
I think "/external/images/media/62" its a Uri not a real file path..
So instead of
final Bitmap b = BitmapFactory.decodeFile("content://media"+getFileLocation(), options);
Just try
final Bitmap b = BitmapFactory.decodeStream(getContentResolver().openInputStream("content://media"+getFileLocation()), null,options);
And let me know what happen..

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

Android: showing default image in gallery's ImageView while actual image is downloaded

I believe this is pretty trivial but I can't get it to work.
I want to display a default image in gallery elements (ImageViews) while their actual image is being fetched from the net.
Right now, nothing is shown for an ImageView which its image has yet to arrive. Once it arrives it is immediately shown.
What I tried is right after the instantiation of the ImageView to call its setImageResource function like so:
final ImageView i = new ImageView(mContext);
i.setImageResource(R.drawable.loading);
But it doesn't seem to work. Below is the full getView() function.
Any help is appreciated.
Thanks.
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView i = new ImageView(mContext);
i.setImageResource(R.drawable.loading);
// if the drawbale is in the buffer - fetch it from there
Drawable bufferedImage = DataManager.getInstance().getImagesBuffer()[position];
if (bufferedImage != null){
i.setImageDrawable(bufferedImage);
BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
drawable.setAntiAlias(true);
}
// if drawable is not in buffer - fetch it from the net via AsyncImageLoader
else
{
String imageUrl = DataManager.getInstance().getImageBufferInstance().getImageUrl(position);
Drawable downloadedImage = AsyncImageLoader.getInstance().loadDrawable(imageUrl, new ImageCallback() {
public void imageLoaded(Drawable imageDrawable, String imageUrl) {
if (imageDrawable == null)
{
imageDrawable = getResources().getDrawable(R.drawable.icon);
}
i.setImageDrawable(imageDrawable);
BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
drawable.setAntiAlias(true);
}
});
i.setImageDrawable(downloadedImage);
}
i.setLayoutParams(new CoverFlow.LayoutParams(Utils.getInstance().getScreenWidth() / 2,
Utils.getInstance().getScreenHeight() / 2));
i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
return i;
}
Fix your indentation... If I'm reading that correctly, you're only setting the temporary drawable icon in the imageLoaded callback of your (not shown) AsyncImageLoader, which I assume means it's then only being set after the image downloads and is then immediately overwritten with the downloaded image. Try moving the placeholder-setting code into your else block outside the callback.

Categories

Resources