I am selecting an image from gallery.I want to determine the size of the image programatically in kb or mb. This is what I have written:
public String calculateFileSize(Uri filepath)
{
//String filepathstr=filepath.toString();
File file = new File(filepath.getPath());
// Get length of file in bytes
long fileSizeInBytes = file.length();
// Convert the bytes to Kilobytes (1 KB = 1024 Bytes)
long fileSizeInKB = fileSizeInBytes / 1024;
// Convert the KB to MegaBytes (1 MB = 1024 KBytes)
long fileSizeInMB = fileSizeInKB / 1024;
String calString=Long.toString(fileSizeInMB);
return calString;
}
The uri of the image when selected from gallery is coming perfectly.But the value of fileSizeInBytes
is zero.I am calling this method on onActivityResult
,after selecting the image from gallery.I saw a few same questions asked here before.But none worked for me.Any solution?
How to reduce the image size in KB/MB? To reduce the image size in KB or MB online, first upload it to ResizePixel's website. Enter a desired file size and select the corresponding unit of measurement (KB or MB). Then proceed to Download page to get the image file.
Or, if you are extracting the images directly from the gallery with a Cursor, you could access the size of the image from the android.provider.MediaStore.MediaColumns - SIZE column. Thanks for contributing an answer to Stack Overflow!
Here first the uploaded Image file is read into an Image object and its dimensions i.e. Height and Width are determined. Then using the ContentLength property of the PostedFile, the size of the File is calculated in Kilobytes (KB).
Simply select your image file, and it will determine the picture dimensions and resize it to a 20kb image by shrinking the size to less than 1000px. Our browser-based tool runs on a cloud server, so no software or login is required. To resize pic, Choose your Photo file and upload it to online compress image to 20KB.
Change
public String calculateFileSize(Uri filepath)
{
//String filepathstr=filepath.toString();
File file = new File(filepath.getPath());
long fileSizeInKB = fileSizeInBytes / 1024;
// Convert the KB to MegaBytes (1 MB = 1024 KBytes)
long fileSizeInMB = fileSizeInKB / 1024;
String calString=Long.toString(fileSizeInMB);
to
public String calculateFileSize(String filepath)
{
//String filepathstr=filepath.toString();
File file = new File(filepath);
float fileSizeInKB = fileSizeInBytes / 1024;
// Convert the KB to MegaBytes (1 MB = 1024 KBytes)
float fileSizeInMB = fileSizeInKB / 1024;
String calString=Float.toString(fileSizeInMB);
When you use long
it will truncate all the digits after .
So if your size is of less than 1MB you will get 0.
So instead use float
in place of long
Just try this one and it will work for you
private void getImageSize(Uri choosen) throws IOException {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), choosen);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
long lengthbmp = imageInByte.length;
Toast.makeText(getApplicationContext(),Long.toString(lengthbmp),Toast.LENGTH_SHORT).show();
}
And on result
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == RESULT_OK){
Uri selectedImage = data.getData();
if(selectedImage !=null){
img.setImageURI(selectedImage);
try {
getImageSize(choosenPhoto);
} catch (IOException e) {
e.printStackTrace();
}
//txt1.setText("Initial size: " +getImageSize(choosenPhoto)+ " Kb");
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With