Android:Load a Scaled Down Image into Memory - android

I used to use BitmapFactory.Options, inSampleSize and inJustDecodeBounds to load a scaled image in memory and not the sample Image...`Is this method load a scaled image in memory?
public void ScaledBitmap() {
Bitmap bMap;
ImageView iv;
iv=(ImageView)findViewById(R.id.my_image);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize=4;
options.inJustDecodeBounds=true;
bMap=BitmapFactory.decodeResource(getResources(),R.drawable.my_image,options);
iv.setImageBitmap(bMap);
}

You have set options.inJustDecodeBounds=true;. This means that the decoded bitmap will be null. So bMap will be null.
UPDATE: When you give inJustDecodeBounds=true and decode the resource, you won't get any bitmap. This option is used only to get the real width and height of the image resource without decoding the image. Using this width and height, you can calculate the inSampleSize. Once you have calculated that, set the inSampleSize and set inJustDecodeBounds=false and decode the resource again. Now you will get the correct scaled bitmap.
You will find more info on how to effectively scale a bitmap here : http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Related

Getting image dimensions in pixels in ImageView

While I try to get image dimensions in pixels in an ImageView, I found that its width is 3 times more than the original jpg file width.
I put a jpg file which dimensions are 800 x 600, but the code below displays 2400 as its width.
Bitmap bitmap = ((BitmapDrawable)imgv.getDrawable()).getBitmap();
float fwidth = bitmap.getWidth();
Log.d("width0", Float.toString(fwidth));
I checked the jpg file size again but it was not changed (800 x 600),
I also searched for a solution but the code above displays the correct dimensions of the bitmap on other user's experience.
What have I done incorrectly?
Can anyone give me some advice?
Thanks for your help.
This is the solution found in a web site.
I needed to change the Option value when I decode the resource not to scale the original image. I had to use the three parameters for the decodeResource function, not two.
Of course, the third parameter was Options specifying the original bitmap not to be scaled. So now I can get 800 when I call bitmap's getWidth() function.
Resources res = getResources();
int id = R.drawable.map10;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
// options.inSampleSize = 3;
Bitmap bb = BitmapFactory.decodeResource(res, id, options);
float fwidth = bb.getWidth();
Make sure, that your ImageView is set to:
height : wrap_content
width : wrap_content
scaleType: none

How to know the right dimension to load a large Bitmap image in android?

Since that loading large bitmaps to show on a device screen in android(the correct way) is not a trivial task, I took a look on some tutorials on how to effectively make it. I'm already aware that you need to do the following in order to make a memory efficient image loader method:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; //do that to avoid loading all the information on the heap to decode
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
Acoording to the google tutorial you should make a sampled sized image, until this point I understand, you should make a method like this:
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
And Finally you call the method like this:
imageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), drawable.big_image, 200, 200));
And here is where my doubt lies: How to know the best size values according to the devices's screen size/resolution ? Is there any method that I can embed on my code which returns the optimal screen resolution to load an image without the pixelated effect and yet not too big that would blow up the VM heap ? This is one of the biggest challenges I'm facing on my project right now. I searched this link(and others I don't remember) but I couldn't find the answer I'm looking for.
As a suggestion ,
You can define the approx : width and height of your imageview as a portion of
device width and height in pixels. As ex:
imageview width = 0.8 of device width & imageview height = 0.4 of device height
Then you can calculate the device width and height in pixels. There by you can get the actual required imageview size according to relevant portion.
Then you can pass the calculated imageview width and height in to "decodeSampledBitmapFromResource" method.

Android draw image with width 1029 * height 1029 pixel

In my Android application, I need an image with size of width 1029 * height 1029 pixel (image center crop is allowed) . The original image width and height is above than 1029 pixels.
I have tried the following things.
I have placed the image in a image view and capture the bitmap I got the following exception.
Bitmap too large to be uploaded into a texture (2322x4128, max=4096x4096)
This is the layout design of image view.
<ImageView
android:id="#+id/iv_try1"
android:layout_width="425dp"
android:layout_height="425dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="centerCrop"
android:contentDescription="#string/icd" />
( I am not sure 425dp gives 1029 pixels, just a try).
I used the following code to capture the bitmap from image view.
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
// resultant bitmap is save into save in SDcard.
How can I get an image with width 1029 * height 1029 pixel resolution as bitmap? Thanks in advance.
as the exceptions states your bitmap is too large. The height exceed the maximum value. What you can do is to downscale the bitmap. The first step would be to load only the meta-info of your bitmap with
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decode*(..., o);
setting inJustDecodeBounds = true, will make the decode* method return a null Bitmap, but the BitmapFactory.Options will contain the width/height of the bitmap in pixels. Knowing those two, you can calculate the value of inSampleSize that you need to scale the bitmap to with/height close to your constraint (1029 px). From the documentation of inSampleSize
If set to a value > 1, requests the decoder to subsample the original
image, returning a smaller image to save memory. The sample size is
the number of pixels in either dimension that correspond to a single
pixel in the decoded bitmap. For example, inSampleSize == 4 returns an
image that is 1/4 the width/height of the original, and 1/16 the
number of pixels. Any value <= 1 is treated the same as 1. Note: the
decoder uses a final value based on powers of 2, any other value will
be rounded down to the nearest power of 2.

Calculate Bitmap size from decoded bounds

Is it possible to extimate a Bitmap's size in memory before actually decoding it? I was running into OutOfMemoryErrors and wouldn't want to try an allocation if there's not enough heap space left. If I set BitmapFactory.Options.justDecodeBounts to true I will get the width and height of the resulting Bitmap:
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, o);
Log.d(getClass().getName(),"Input image size: "+o.outWidth+"x"+o.outHeight);
Is there an easy way to get to the Bitmap's size without having to calculate it from the dimensions?
I was going to work around the issue by multiplying width*height*4 for ARGB_8888 to get the byte count of the resulting image. This is fairly precise but the resulting Bitmap's byte count is a bit higher than that. How would I go about and take this overhead of the Bitmap Object into account? I am not planning to re-use the bitmap so this should hopefully work with KitKat's getAllocationByteCount().

Is the image size and bitmap size equivalent in android?

I am little bit confusing because i am calculate the size of of an image.
I am using the following code in android:-
String fileUrl = getIntent().getExtras().getString("fileurl");
BitmapFactory.Options option = new BitmapFactory.Options();
option.inPurgeable = true;
Bitmap bitmap= BitmapFactory.decodeFile(fileUrl,option);
Log.e("Fill Image Size in Bytes","====>"+bitmap.getByteCount());
The above function bitmap.getByteCount() return different value compare to original size of image(compare with right click of image size in ubuntu).
If anyone have idea.please reply.
Thanks in advance...
You can decode the bitmap without options to see what's the difference.
Bitmap bitmap= BitmapFactory.decodeFile(fileUrl);
Log.e("Fill Image Size in Bytes","====>"+bitmap.getByteCount());
the default value of options may scale down the size.
It may be decided by Bitmap.config and bitmap size.

Categories

Resources