3

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.

user228989
  • 31
  • 4
  • see this http://stackoverflow.com/questions/13328673/calculate-image-size-based-on-screen-size-in-android and post the code that u have till now – Developer Sep 04 '13 at 10:57
  • Visit the following link may help you out- http://stackoverflow.com/a/13328803/2515750 – FraZer Sep 04 '13 at 11:00

2 Answers2

0

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?

Community
  • 1
  • 1
Alexey Abraham
  • 379
  • 2
  • 13
0

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();
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57
  • Thank you for your answer. But, I want to calculate the size of Image while selecting the image from gallery. – user228989 Sep 05 '13 at 10:16