setImageUri on ImageView doesnt work - android

I am trying to make a picture with my app but it doesn't work.
I'm implementing a function to record a picture with my app, the result of this is a URI.
This URI I have in my activity, with this I try to show the user the recorded picture, to realize this I use from the imageview the method setImageUri ... but it doesn't work for me.
The thing I'm confused about, the imageview exist, in the screen I see the dimension.
In this case, the code that I have implemented
ImageView imgView = new ImageView(activityContext);
imgView.setImageURI(Uri.parse(captureImg.toString()));
mainFrame.addView(imgView, 100, 100);
another solutions:
Drawable d = Drawable.createFromPath(captureImg.toString());
ImageView imgView = new ImageView(activityContext);
imgView.setImageDrawable(d);
mainFrame.addView(imgView, 100, 100);
This both doesn't work for me, I have to try to use any solutions with Bitmap but this doesn't work me too.
Greets

Please make sure your "UI code" (imgView.setImageDrawable(d);) run on UIthread
Take this for reference: http://www.vogella.com/articles/AndroidPerformance/article.html

Try as:
ImageView imgView = new ImageView(activityContext);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams
(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
imgView.setLayoutParams(vp);
imgView.setMaxHeight(100);
imgView.setMaxWidth(100);
imgView.setImageURI(Uri.parse(captureImg.toString()));
mainFrame.addView(imgView);
you are not adding LayoutParams to dynamically created ImageView

---Try this---
ImageView imgView = new ImageView(activityContext);
imgView.setImageURI(Uri.parse(captureImg.toString()));
mainFrame.addView(imgView, new LinearLayout.LayoutParams(200,200));

Related

how to removing programmatically composed pictures

I create imageview programmatically. but after I finish game this imageview cannot close how can i close, remove this imageview.
if I use this code image.setImageResource(0); just last image closing but I want to close all image
final ImageView image = new ImageView(getContext());
image.setImageResource(id);
image.setLayoutParams(params);
layout.addView(image);
Great!!! This is my solution..
` final ImageView [] image = new ImageView[5];
`
image[userartis]=new ImageView(getActivity());
image[userartis].setImageResource(id); image[userartis].setLayoutParams(params);
layout.addView(image[userartis]);
for (int i=0;i<=userartis;i++){
image[i].setVisibility(View.GONE);
}

Covering android ImageView

While I was working on my project I found that the image couldn't cover the whole space of ImageView completely! this is the first time that i have got a problem like this.
Here is my code :
int ImgId = getResources().getIdentifier("test1", "drawable", getPackageName());
Photo=new LinearLayout(this);
lp = new LinearLayout.LayoutParams(Width/2,Width/2);
Photo.setLayoutParams(lp);
Photo.setOrientation(LinearLayout.VERTICAL);
Photo.setBackgroundColor(getResources().getColor(R.color.mainGreen));
Img = new ImageView(this);
lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
Img.setLayoutParams(lp);
Img.setBackgroundColor(getResources().getColor(R.color.Black));
Img.setImageResource(ImgId);
Photo.addView(Img);
I have used this piece of code many times before. The image covered the ImageView's space but now its not working well!
What is the problem? How can I cover ImageView completely?
Img.setBackground(ImgId);
//Img.setImageResource(ImgId);
or
Img.setBackground(ImgId);
Img.setScaleType(ImageView.ScaleType.FIT_XY);
Img.setImageResource(ImgId);
Just add ScaleType to the ImageView to specify the behavior
Img.setScaleType(ImageView.ScaleType.CENTER_CROP);

Android SDK - convert bitmap to resource for layout params issue

For some reason, when I create a bitmap from an image in asset, it does not display fullscreen. When I take an image from drawable, resId, the image shows fullscreen. Why is this?
//the image is not fullscreen
ImageView iv = new ImageView(getBaseContext());
iv.setImageBitmap(bitmapFileFromAssetorSD);
//this makes the image fullscreen
//iv.setBackgroundResource(resId);
iv.setLayoutParams(
new LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT
)
);
Try setting the appropriate ImageView.ScaleType
ImageView.setScaleType(ImageView.ScaleType.CENTER_CROP)

Copy Bitmap contents of one ImageView to anoher

This has me baffled. I need to copy the Bitmap from one ImageView into another. I do not want to simply copy one ImageView to another because I need to do some changes to the bitmap on its way over.
Here is some code that doesn't work.
ImageView ivSrc = (ImageView) findViewById(R.id.photo);
ivSrc.setDrawingCacheEnabled(true);
Bitmap bmSrc1 = ivSrc.getDrawingCache(); // will cause nullPointerException
Bitmap bmSrc2 = Bitmap.createBitmap(ivSrc.getDrawingCache());//bmSrc2 will be null
View vSrc = (View) ivSrc.getParent();
vSrc.setDrawingCacheEnabled(true);
Bitmap bmSrc3 = Bitmap.createBitmap(vSrc.getDrawingCache()); //black bitmap
//To test the bitmaps:
ImageView ivDest = (ImageView) findViewById(R.id.photo2);
ivDest.setImageBitmap(bmSrc1); //bmSrc1, 2, 3 results shown above
I have to going about this wrong because doing a copy should be so easy. TIA
Not used the drawing cache, but wouldn't you need to call buildDrawingCache() ?
The way I'd do it:
Bitmap bmSrc1 = ((BitmapDrawable)ivSrc.getDrawable()).getBitmap();
Bitmap bmSrc2 = bmSrc1.copy(bmSrc1.getConfig(), true);
Note that bmSrc2 is mutable, i.e. you can stick it in a Canvas and do whatever you like with it before drawing it somewhere.

How to set gravity (or margins) of ImageView using code?

I want to add ImageView to FrameLayout with Gravity or margins. but FramLayout and ImageView has no method about that(Actually, I can't found that). Reason that selects Framelayout is to put ImageView on ImageView.
Help me plz. It is emergency for me to find solution.
thx.
Below is my code which help understanding my question.
FrameLayout imageFrame = new FrameLayout(mContext);
imageFrame.setLayoutParams(new GridView.LayoutParams(158, 158));
ImageView frame = new ImageView(mContext);
frame = new ImageView(mContext);
frame.setLayoutParams(new LayoutParams(158, 158));
frame.setScaleType(ImageView.ScaleType.CENTER_CROP);
frame.setImageResource(R.drawable.image_frame_n);
ImageView image = new ImageView(mContext);
image.setLayoutParams(new LayoutParams(148, 148));
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
image.setImageResource(mThumbIds[position]);
// image is needed to have a margin or gravity to be positioned at center of imageFrame
imageFrame.addView(image);
imageFrame.addView(frame);
Try:
image.setLayoutParams(new FrameLayout.LayoutParams(width, height, gravity));
More info:
http://developer.android.com/reference/android/widget/FrameLayout.LayoutParams.html

Categories

Resources