android - fitting image in imageview - android

I'm implementing a widget where i'm trying to display a large image inside an image view (8mpx) like this :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" android:id="#+id/widget"
android:background="#000000"
android:padding="15dp"
android:layout_margin="5dp"
android:layout_gravity="top|center_vertical|center_horizontal"
>
<LinearLayout android:background="#ffffff" android:padding="1dp" android:layout_width="wrap_content" android:layout_weight="1"
android:layout_height="wrap_content" >
<ImageView
android:adjustViewBounds="true"
android:id="#+id/image"
android:src="#drawable/sample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="top|center_horizontal"
android:scaleType="fitXY"
android:maxWidth="200dip"
android:maxHeight="300dip"
/>
</LinearLayout>
In the emulator everything seems ok, but when i deploy to device, i get the "problem loading widget" message.
the emulator is HVGA and my device has a 480x800 resolution.
Any ideea what am i doing wrong ?
Thank you!
==================================================
As advised by you guys i've made a screenshot of the logcat.
Here it is :

imageview.setScaleType(ScaleType.CENTER_INSIDE);

Try below code
<ImageView
android:adjustViewBounds="true"
android:id="#+id/image"
android:src="#drawable/sample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal"
android:scaleType="centerInside"
/>

use this
imageview.setImageResource(your_image);
imageview.setScaleType(ScaleType.MATRIX);

Old post, but you never know...
The logcat shows the problem:
"allocation too large for this process"
The image you are trying to render is too large to fit into memory. You need to scale the image down, but don't try to create a scaled version of the bitmap or you'll hit the same problem. The solution is the load the Bitmap into memory but ONLY for it's dimensions, then create a new Bitmap with a sample size that reduces the overall size of the image.
Actually you don't even need to load in the image to get it's original size to do this but it often makes sense so you can choose an appropriate sample size.
e.g.
Assuming your Bitmap is obtained from an InputStream:
InputStream in = ... // Your Bitmap Stream
// Decode JUST the dimensions
Options dimensionOptions = new Options();
dimensionOptions.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, dimensionOptions);
// Get the dimensions of the raw
int rawWidth = dimensionOptions.outWidth;
// Choose a target width for the image (screen width would make sense)
int targetWidth = 480;
// Select the sample size which best matches our target size.
// This must be an int
float sampleSize = (float)rawWidth / (float)targetWidth;
// Assume lower, which will result in a larger image
int sample = (int) FloatMath.floor(sampleSize);
// Use this to decode the original image data
Options scaleOptions = new Options();
scaleOptions.inPreferredConfig = Bitmap.Config.ARGB_8888; // 4 bytes per pixel
scaleOptions.inSampleSize = sample;
// Now create a bitmap using the sample size
Bitmap scaled = BitmapFactory.decodeStream(in, null, scaleOptions);

Related

how to fill image fullscreen in android

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imageview"
android:scaleType="centerCrop"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/imagecancel"
android:layout_marginBottom="10dp"
android:background="#80000000">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/imagecaption"
android:hint="Enter a description"
android:textColorHint="#80ffffff"
android:textColor="#ffffff"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"/>
</RelativeLayout>
<ImageButton
android:contentDescription="#string/imagecancel"
android:id="#+id/imagecancel"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:scaleType="fitStart"
android:background="#android:color/transparent"
android:src="#drawable/cancel"/>
<ImageButton
android:contentDescription="#string/imagesave"
android:id="#+id/imagesave"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:scaleType="fitEnd"
android:background="#android:color/transparent"
android:src="#drawable/ok"/>
</RelativeLayout>
Java
private void previewCapturedImage() {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
ExifInterface exif = null;
int orientation = 0;//Since API Level 5
try {
exif = new ExifInterface(fileUri.getPath());
orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
} catch (IOException e) {
e.printStackTrace();
}
String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
Log.i("file path",exifOrientation);
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath());
previewimage.setImageBitmap(bitmap);
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
Log.i("RotateBitmap","270");
RotateBitmap(bitmap, 270);
previewimage.setRotation(270);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
Log.i("RotateBitmap","90");
RotateBitmap(bitmap, 90);
previewimage.setRotation(90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
Log.i("RotateBitmap","180");
RotateBitmap(bitmap, 180);
previewimage.setRotation(180);
break;
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}
public static Bitmap RotateBitmap(Bitmap source, float angle)
{
Matrix matrix = new Matrix();
matrix.postRotate(angle);
previewimage.setImageBitmap(source);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
here image fill normaly in small screen phones. But in large screen phone image didn't show correctly.It shows a gap 2cm from top and 2cm from bottom. The image comes from taking picture from phone camera. I want to fill image fullscreen. How to solve this problem.
try this
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imageview"
android:scaleType="fitXY"/>
The source of your problem is a difference between the side ratio of your current preview area compared to side ratio of the picture you're getting. Your preview area will be different on different devices (even on the same device in different orientation - caused by 'soft buttons' position), and the image you get from the camera has rarely the same side ratio as you current preview.
Your question can be answered without any code. Just draw one rectangle (non-square) and one square on a piece of paper and try to fit one into another.
The rectangle (wide or tall) represents you phone and the square represents the picture you've got from the camera (it mostly isn't square, but I use square here to make it clearer to demonstrate).
If you take these 2 shapes and try to 'fit' one into another, you will end up with 3 different scenarios:
you manage to fit square into rectangle by stretching/squeezing it. This is definitely not the result to shoot for (first, it is not square anymore, second, our grandma's face will be too fat or too skinny :-)
you fit the full square into your rectangle and you'll see 2 unfilled areas (if you center it) - this is the situation you're complaining about. Also called 'fit-in' or 'letterbox';
you fill the full rectangle with a portion of your square and two sides of your square will overflow the rectangle. This case looks like you've achieved your goal if you don't mind that you lost some of your image. Situation is sometime called 'pan&scan'.
So, how does this rant help you? If you insist on filling the full screen (and losing overflowing image portion), you adjust your SurfaceView area after you get your image, use it's width / height to calculate the ratios.
If you really insist on seeing some code, it's done here (see the setLayoutParams), but be warned, the example I'm pointing to is more complicated and involves custom camera handling. But the general idea is the same.
Good Luck
if you want, you can set it as RelativeLayout background in xml.
android:background="#drawable/_image"
or set it dynamically.
Change the scaletype
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imageview"
android:scaleType="fitXY"/>

Android photo in imageview, bad quality

I allow user to take photo, and I get this photo to set it in a imageview.
This is my code for my imageview :
<ImageView
android:id="#+id/photo1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:layout_marginBottom="2dp" />
And my code to put the photo in this imageview :
imgPhoto1.setImageBitmap(btmap);
My problem is, the photo is showing is blurred.....
How can I do to have a "correct" quality ?
I tried to use this :
getWindow().setFormat(PixelFormat.RGBA_8888);
but it changes nothing.
Thx,
EDIT : Please, I'm searching, if possible, a solution without to use a library, it's just for two imageview..
EDIT 2 : Ok, it seems impossible to do without library, because my image take all of space available on screen.
This function resolves the problem of bad quality of a image at ImageView:
public static Bitmap getBitmapFromResources(Resources resources, int resImage) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inDither = false;
options.inSampleSize = 1;
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
return BitmapFactory.decodeResource(resources, resImage, options);
}
And imageView.setImageBitmap(getBitmapFromResources(getResources(), R.drawable.image));
Instead of loading bitmap use universal image loader or picasso for this:
Android-Universal-Image-Loader
picasso
Why use Android Picasso library to download images?
You can also use AndroidQuery. It also allows you to load images async. I never had a quality issue with it. It is pretty easy to use and also allows you to cache images.
https://code.google.com/p/android-query/
change your imgview width and height style
<ImageView
android:id="#+id/photo1"
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:layout_marginBottom="2dp" />
Hope It help

Best way to display an image from server in android

I am trying to display an image in android. Image needs to be taken from the server. What should be the best approach for making sure that the image stored at the server fits with all android device screens. In the image displaying XML I need to display a textview (below the image) also for giving a brief description about the image. Do I have to create the image with a specific height and width or is there any other approach?
You should store a large enough image on the server.
// Know the required width of the image
URL url = new URL(remotePath);
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
inputStream = (InputStream) urlConnection.getContent();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, options);
int height = options.outHeight;
int width = options.outWidth;
int sampleSize = requiredWidth / width; // Calculate how you want to sample the images so you can keep the memory small
options.inSampleSize = sampleSize;
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, options);
imageView.setImageBitmap(bitmap);
Hope this helps.
If you want to display on that screen only the image and description below it, you could place the 2 component in a LinearLayout with the orientation vertical and use the layout_weight property of the LinearLayout, for example you could do something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="center" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
You could download the image from the server manually or you could use a library like UIL, or webImageLoader.

Resizing a bitmap download from url to fit imageview space: lost of quality

I know there are a lot of questions around this subject, but I cannot find something which perfectly fits my problem so I open this question in order to, at least, gather here EVERY aspect of the problem.
Let's start! I have my imageview and this imageview has to have fixed dimensions:
<ImageView
android:id="#+id/my_image"
android:layout_width="250dp"
android:layout_height="170dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:contentDescription="#string/cell_item_image"
android:scaleType="fitXY" />
my bitmap comes from an url and is in the .jpg format, so my code is:
bitmap = BitmapFactory.decodeStream(conn.getInputStream());
its dimensions are 435x312 pixels, bigger than my screen density so scaling it should not lead to a loss of quality...how come I get an obfuscated image?
Is there a way to rescale bitmap and preseve quality?
Maybe it's that fitXY in the scale type? But what's the difference between scaling in code or in layout? I see many does that in code but, trying, I get exactly the same result
EDIT: read over the .jpg part
Try:
Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap source = BitmapFactory.decodeResource(a.getResources(), path, options);
If this doesn't work, try displaying the image without scaling (this way you can see what causes the blur, the loading of the image or the displaying/scaling)

how to reduce large image size to thumbnail size in android

in my app, i have large images, i display it in an image view by hard code the size as 60 X 60 dp. how to reduce the image size as image thumb nail size in android. is there programmatic way. because suppose i run my app in various resolution size design layout may get differ. my need is if i run my app in any resolution device image size should not get differ. how to do that in android. please help me.
i hard code image by the following code.
<ImageView android:id="#+id/img" android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerVertical="true" />
Most simple way is
int h = 48; // height in pixels
int w = 48; // width in pixels
Bitmap scaled = Bitmap.createScaledBitmap(largeBitmap, h, w, true);
and detailed
This is called the image scaling
This will help you http://zerocredibility.wordpress.com/2011/01/27/android-bitmap-scaling/
Add the following elements in your xml file:
android:scaleType="fitXY"
android:layout_width="48dip"
android:layout_height="48dip"

Categories

Resources