Displaying an image from within an Android app? - android

I have filepath which is a String holding the absolute path to an image file.
Is there a way to display the image to the user similar to when they use the Gallery app?
When I looked up solutions to this problem, most of them involved setting an ImageView's bitmap to the bitmap decoded from the file, but I don't want to go this route because the orientations may be different, the size of the picture may not be maximized depending on how it fits within the ImageView, and so on. I basically want to have it displayed full-screen. Or if this can be done with an ImageView I'd be curious to know how.
Is this possible to do?

You can declare an ImageView in your layout that fills the parent
<ImageView
android:layout_gravity="fill"
android:id="#+id/your_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Then you use the Picasso library to load the image by
1/ Inflating the ImageView
yourImage = (ImageView) view.findViewById(R.id.your_image);
2/ Loading the image to the ImageView like
Picasso.with(context).load(filepath).fit().into(yourImage);
Check http://square.github.io/picasso/

Use the following library: TouchImageView
Usage:
Place TouchImageView.java in your project. It can then be used the same as
ImageView. Example:
TouchImageView img = (TouchImageView) findViewById(R.id.img);
If you are using TouchImageView in xml, then you must provide the full package
name, because it is a custom view. Example:
<com.example.touch.TouchImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Related

Android crop imageview

There is my ImageView
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="150dp" />
I load image to this ImageView using Picasso library.
Picasso.with(getActivity()).load(url)
.config(Bitmap.Config.RGB_565)
.into(imageView);
There is problem that there is padding inside ImageView and loaded image.
There is image to show padding:
I use .fit() method to avoid this gap, but image stretches and quality losts.
(You can see that this dark-blue circles is ovals now)
The question is:
What should I do to avoid loosing quality of image and resize it directly to ImageView. I can't add .centerCrop() because left side of image is logical safe-zone. Also I can't change layout_height="150dp" because it will break all my layout.
Anyway thank you!
Look at your ImageView:
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="150dp" />
You are forcing the image to stretch to the width of its parent view (the screen maybe?). Try using wrap_content for the width and keeping the height the same. This may cause the image to shrink, but its up to your desired UX.
#stefs found that adding this:
...android:adjustViewBounds="true" />
would help. Check out the post here.
If you want static image sizes, you may have to deal with the padding.

Using a png image format on android devices

Whenever i am using transparent PNG images, on my android device ( nexus ), i get a border on one side of transparent part. What is the way to sort out this problem ?
The image you are trying to add may not be transparent.Check it...
In the xml file, use this code to add an ImageView and set a resource for it:
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sample" />
To access the ImageView from the java code ,use the following code.
ImageView imgView = (ImageView) findViewById(R.id.image);
You can also set a resource programatically:
imgView.setImageResource(R.drawable.sample);

Image stretches horizontally

I am developing an app based on client-server application. I am using tablelayout, and in tablelayout am adding some textview, edit text and image( for click ). My screenshot is as follows:
as you can see in my image add button blurs. It is just a 3kb file.
final ImageView img_add_icon = new ImageView(MainScreen.this);
img_add_icon.setBackgroundResource(R.drawable.add_item_order);
and my xml file is:
<ImageView
android:id="#+id/add_to_order_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dip"/>
What can I do to stop the blurring of this image?
Please Use
setImageResource(resId)
to Sets a drawable as the content of this ImageView.
To Control how the image should be resized or moved to match the size of this ImageView
use
setScaleType(ImageView.ScaleType scaleType)
[1]: http://developer.android.com/reference/android/widget/ImageView.html#setImageResource%28int%29
[2]: http://developer.android.com/reference/android/widget/ImageView.html#setScaleType%28android.widget.ImageView.ScaleType%29

Android ImageView dynamic ScaleType

I have an Android Activity with an ImageView.
I put the ScaleType of the imageview on CENTER_CROP, so the image itself would not stretch.
<ImageView
android:id="#+id/imgView"
android:layout_width="fill_parent"
android:layout_height="135dp"
android:scaleType="centerCrop"
android:src="#drawable/capture" />
The image that's loaded at first does what I expect, it's cropped to the center, filling the whole ImageView.
Now, if I set the image of the ImageView programatically,
imgView.setImageBitmap(b)
imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
The picture gets displayed, but it's completely stretched out to fill the ImageView...
AdjustViewBounds has no effect what so ever, nor does anything else.
The difference between the working XML and the not-working java code is that in XML I'm putting the image as a resource, and the code is putting a Drawable in the ImageView from a web-service...
Does anyone have an idea to the solution?
I am not sure but android:scaleType="fitXY" or android:scaleType="fitCenter" may be the solution of your problem.
I solved this by using the original image from my camera-intent. For some reason, all thumbnail bitmaps were stretched out and in landscape mode.

Custom picture placed in Layout

Is there any way to create a custom image, and then place it into a layout you have?
Yes, you can use an ImageView.
Example xml:
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_image"/>
Then you would just stick my_image.png into res/drawable.
Edit - to draw dynamically you want to subclass View and override onDraw(Canvas) with a custom drawing method.
The other option is to load multiple images into res/drawable and choose between them in your Java code:
ImageView image = (ImageView) findViewById(R.id.image);
image.setBitmapResource(R.id.some_image);
Just what Matthew said. Also if you meant to place the image as the layout background you can do it like
<android:background="#drawable/background">
where "background" is an image like background.png placed in the res/drawable folder.

Categories

Resources