android loading bitmap efficiently - android

I want to load bitmap efficiently, because frequently on 4.0+ android i get OutOfMemoryError. So i tried to use sample code from android developers page resource "Loading bitmaps efficiently".
However my problem is to get height and width of ImageView, because dont have any constant values to define dimensions.
Code with required fields from developer page.
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight)
Here is my layout:
<RelativeLayout android:layout_width="match_parent"
android:layout_weight="3"
android:layout_height="0px">
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
android:src="#drawable/image" />
I tried to use image.getHeight() but that didnt work.

You can use a ViewTreeObserver and wait for one of the callbacks, and then grab the size of the ImageView.
See more here:
When Can I First Measure a View?

This is because 'image' is not an ImageView object. Here is how to programmatically declare an ImageView in your case :
`ImageView image = (ImageView) findViewById(R.id.img1);
Moreover, this might help you: How to retrieve the dimensions of a view?

Related

Is it possible to display original image in android without fit to device views?

I have an Image of size 1000px*1200px and i want it to be displayed without being scaled!.
I have searched in stackover flow and google i am not getting good one.
I want full image without scaled,
I have tried like this
Step1:-
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image"
/>
Step2:-
I have tried with scaleType also
add a scaleType to your ImageView
<ImageView
android:scaleType="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image" />
But this is also not working,
In sclate type i have tried with all possible properties (matrix,fitXY like this).
is there any possibility there for doing to dispaly original image in android?
Plz help me on this
You can try this approach,first get image from drawable and create a bitmap of it.Then get width and height of bitmap, resize your image view and then display image in it.It will create imageview with your original image size.
See below code -
BitmapDrawable bitmap = (BitmapDrawable) this.getResources().getDrawable(R.drawable.icon);
int bitmapHeight= bitmap .getBitmap().getHeight();
int bitmapWidth = bitmap .getBitmap().getWidth();
Now you have dimension of your original image.
Re-size image view -
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
imageView.getLayoutParams().height = bitmapHeight;
imageView.getLayoutParams().width = bitmapWidth;
and in last step set bitmap to your image view -
imageView.setImageBitmap(bitmap);
Hope it will help you.
EDITED
Use below code after resize image view -
replace - `imageView.setImageBitmap(bitmap);`
by
imageView.setImageResource(R.drawable.icon);
By your comments, I think what you need is a library like PhotoView.Hope it can help you
Although I would highly condemn such UX but whatever works for you.
1) Use the following link to make a two dimensional scroll.
Two dimensional scrolling... a suggestion that needs feedback
2) Add an imageview to the same and set your image dimensions for the imageview in px at runtime.
as far as i understand your question is that u want natural size of image without ImageView tag modifying it. This worked for me
<LinearLayout
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/image_here"
android:orientation="horizontal|vertical"/>
let me know in the comments.
Make your image layout width and height to wrap content. Make scaleType centre. Check your device width and height through device manager then give image height and width in %
After many trails i have find a solution for above question.
Here is the code:
MainActivity:-
public class MainActivity extends ActionBarActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) this.findViewById(R.id.imageview);
}
}
activity_main.xml:-
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="#+id/ScrollView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none"
xmlns:android="http://schemas.android.com/apk/res/android">
<HorizontalScrollView android:id="#+id/HorizontalScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="#+id/imageview"
android:src="#drawable/desert"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:adjustViewBounds="true">
</ImageView>
</HorizontalScrollView>
</ScrollView>
This is working perfectly for my requirement.
Hope this helps to others.

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

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)

Swapping Image in ImageView

I've been trying to implement simple (I think) thing in my app. I thought it would be simple, but somehow it doesn't work as I would wanted it to.
What I'm trying to do is:
There is an activity with ImageView in center - it has it's own image (well, there's two of them).
I want to press that ImageView (or it could be ImageButton), open the gallery/take picture with camera.
Then, after taking/picking a picture, I want to make that ImageView display the thumbnail of picture.
My layout
<ImageView
android:id="#+id/add_photo_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:src="#drawable/add_photo_button" />
<ImageView
android:id="#+id/add_photo_right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:src="#drawable/add_photo_button" />
</LinearLayout>
My code - after getting picture path
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/5.png",bmpFactoryOptions);
lastClicked.setImageBitmap(bitmap);
//lastClicked.setImageResource(R.drawable.green_circle);
What I wanted to do is to read the resized file (using inSampleSize) from sdcard, and then fill ImageView (or ImageButton) with it.
But after using setImageBitmap function, the view disappears - like it just loaded empty image (commented line works properly - it's just green dot).
Maybe someone has approached similar problem? I would much appreciate any kind of help.
Greetz,
scana
EDIT:
ofc lastClicked:
lastClicked = (ImageView)findViewById(R.id.add_photo_left);
Why don't you try lastClicked.invalidate() ?
I do so in my code at least and it works this way ;)

android - fitting image in imageview

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

Categories

Resources