How to calculate selected image size in gallery? - android

Have edited my question to make it more clear.
Basically I am working on an App where users need to upload their profile image. However I need to limit the size of the image upload to less than 4MB. Here where user selects his image from the image gallery, at that very instant I need to check the image file's size and show an alert to the user if the file size is greater than 4MB and restrict the upload.
Presently I am using this code to get the file size:
File Img = new File(selectedImage.getPath());
int length = Img.length();
I know length() returns file size in bytes, however even after conversion from bytes to MB, this always seems to return very small values than the original image file size leading to me believe that getting the file size this way is inaccurate.
Is there any other way I can get the file size of the image files from the device gallery?
Any help is much appreciated. Thanks guys.

Actually, the question is little bit unclear: size is 'in Kb' or youhave to know how would it seen on the screen??
Will that help?
Determining image sizes for multiple android screen sizes/densities
Android screen sizes in Pixels for ldpi, mdpi, hpdi?

Here's how to get dimensions from a drawable resource with BitmapFactory:
BitmapFactory.Options o = new BitmapFactory.Options();
o.inTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
Bitmap bmp = BitmapFactory.decodeResource("Your image file");
int w = bmp.getWidth();
int h = bmp.getHeight();

Related

Resize a bitmap eficiently and without losing quality in android

I want to reduce the size of an image on disk without losing quality. I don't want to change the width and height of original image.
I want to do something like https://tinyjpg.com/
It reduces the size of image without changing the width and height of image.
You need to compress with jpeg, if you want to reduce size without change measurements. Try this;
String file = ...
OutputStream s = new FileOutputStream(file);
BitmapFactory.decodeFile(file).compress(CompressFormat.JPEG, 80, s);
s.close();
80 is compression factor which between 0-100 (0 lowest quality, 100 highest quality). Try to find best for your situation.

Images scales defferently in setImageDrawable() and setImageBitmap()?

I am new in android development. I have an issue regarding the image scaling in my app. When i set images using setImageDrawable() images look proper like in screenshot:
But if i set image using setImageBitmap() method it does not look proper. Some black color occurs around it like in screenshot bellow:
I have tried it in multiple applications and i observed that in some applications the image size is becoming smaller than the size ofter setting image through setImageDrawable() or setImageResource().
the reason for setting images through setImageBitmap() is that i want to set images to the imageviews which are kept in assets folder and not in drawable.
Following is the code i used to create a bitmap and set it to imageview:
BitmapFactory.Options opts = new Options();
opts.inPurgeable = true;
try
{
Bitmap preview_bitmap = BitmapFactory.decodeStream(assetManager.open(drawableFolder+"/"+tmpArr[0]),null,opts);
view.setImageBitmap(preview_bitmap);
}
catch (Exception e)
{
LNLog.writeTOErrorToLog(e.getMessage());
}
So, please can anyone tell me where i'm i going wrong or missing something to set.
I tried to set Options but did not worked. I want to set images as in first screenshot that is how they looks by using setImageDrawable().
Thanx in advance.
Note: in screenshots images are referent in text on it only.IF i set hindi images i.e. the images on which text is in hindi language using setImageDrawable() then they looks proper as in like in screen shot one.So , no issue of image files.
You can get rid of the black borders by using
android:adjustViewBounds="true" in xml
or
imageView.setAdjustViewBounds(true) in Java.
EDIT:
Then try using drawable only
Drawable d = Drawable.createFromStream(assetManager.open(filePath), null);
view.setImageDrawable(d)
This worked for me :
try
{
view.setAdjustViewBounds(true);
int height = view.getDrawable().getIntrinsicHeight();
int width = view.getDrawable().getIntrinsicWidth();
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPurgeable = true;
opts.inScaled = true;
Bitmap preview_bitmap = BitmapFactory.decodeStream(assetManager.open("drawable-mdpi/FAQ_hi_in.png",null,opts);
Bitmap bmp = Bitmap.createScaledBitmap(preview_bitmap, width, height, true);
view.setImageBitmap(bmp);
}
In this code I have taken the height and width of actual image and then scaled my bitmap to that height and width. Then I converted this scaled bitmap to drawable and set it into my ImageView, thats it. It resolved my problem of black border around image.

Android OutOfMemoryError while using multiple images

In my application, i have 3 screens containing multiple high resolution images. The number of images used in a screen is around 70-75. I have written the code to add images in a grid layout using an adapter class extending BaseAdapter, in the getView() method i wrote the code,
adapter = new ImageAdapter(this);
gridview.setAdapter(adapter);
int x = (int)(width/5.1f);
imageView.setId(position);
imageView.setLayoutParams(new GridView.LayoutParams(x,x));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(4, 20, 4, 20);
but while loading this screen, it show lots of memory issues, and in logcat i am getting the error,
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
Please share how could i write the code to handle memory issues with multiple high resolution images. Thanks.
If there is no way to adjust the images resolutions you should open them as BitmapFactory.Options().inJustDecodeBounds = true, pass your options to the images (desired adjusted size) and then decode them again using BitmapFactory.Options().inJustDecodeBounds = false.
The actual byte size of a bitmap image is calculated by multiplying the number of pixels by then number of bytes allocated for the pixel. ARGB_8888 (which is recommended) allocates 4 bytes per pixel, therefore, the size will be width * height * 4 Bytes.
For more details read the Loading Large Bitmaps Efficiently lesson from Android. Also this post should help.

Image size issue when loading from external storage on Android

I would like to display an image from external storage which is 72x72 px.
My device has high density.
The image view height and width are "wrap_content".
I got different results if I load the image or use an URL.
If I use an URL like this then the result will be about 48x48px.
imageView.setImageURI(Uri.fromFile(file));
If I load the bitmap the result is 72x72 px as expected:
InputStream is = context.getContentResolver().openInputStream(Uri.fromFile(file));
Bitmap b = BitmapFactory.decodeStream(is, null, null);
is.close();
iv2.setImageBitmap(b);
You can see the results here:
It would be better if I could use the setImageURI and not to preload
the image and I would like to display the image in appwidgets too.
Can you tell me what cause the difference and how can I avoid it?
try by this i think it work
either change in xml file in imageview by
android:scaleType="centerCrop"
android:adjustViewBounds="true"
or either use in java code
image.setAdjustViewBounds(true);
image.setScaleType(ScaleType.CENTER_CROP);
either use "centercrop" or "fitxy" may it also works
If both images have different sizes and you can't change the image size you have to set in your xml the image width and height to for example 72dp. You shouldn't use px, but dp because otherwise it wouldn't display good on different phones.

android - query CAMERA folder images with width and height

is it possible to query for images from CAMERA folder with height and width?
I am querying it with only URL right now. I want to avoid loading image into Bitmap and I need to have width and height of it beforehand so I can figure out proper scaling. Right now some of the photos I load can be really large size and some can be tiny. If I set same scaling on them they don't look right. Small images get resized for no reason. Knowing width and height would solve this. Also I cannot sample load a bitmap because that causes memory issue when images are really large, like those taken with 3 megapixel camera.
Here is how I load them:
https://stackoverflow.com/questions/5710036/android-camera-folder-images-not-all-show-up
Thank you.
You should do decoding for the image without getting its whole data.
the reason it doesn't exist on the DB is that the user can always modify the images to have different dimensions than the original one.
in order to get just the minimal information of the bitmap, use something like:
public static BitmapFactory.Options getBitmapOptions(final String filePath) {
final BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, bitmapOptions);
return bitmapOptions;
}

Categories

Resources