i am working on an image gallery app in which i am loading images from internet and i am saving images to Sd Card on button click.
now what i want is i want to get the size of the image that is loaded in ImageView. i want to get image size in KB / Bytes.
i want to use image size to prevent duplicate save and replacing image.
this is my code that i am using to save image: same resolution images get replaced
int intHeight = fullImageView.getHeight();
int intWidth = fullImageView.getWidth();
String dirname2 = "/Wallpapers/";
File myDir2 = new File(Environment.getExternalStorageDirectory()
.getPath() + dirname2);
myDir2.mkdirs();
String fname2 = "image" + intHeight + intWidth +".jpeg";
File file2 = new File(myDir2, fname2);
if (file2.exists())
file2.delete();
try {
FileOutputStream out = new FileOutputStream(file2);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
Toast.makeText(_context, "failed", Toast.LENGTH_SHORT).show();
}
Please Note: i am asking about image size in KB or Bytes
Don't use image sizes as a validation for similarity. Instead compare the images. The simplest way is to iterate over every pixel and compare the RGB value. But this will give you only a valid answer if the images are really to 100% equal. If you like to detect similarity based on the image, independed of the resolution, you should search for a libary to compare images.
edit2: As mentioned, it would take a while to 'compare' the images by their structure. In this case, I would suggest to store the url, name and etag in a database and save the images via a UUID and make a db lookup (fast) if you want to save an image. But don't reinvent the wheel. Just use a libary that is able to handle this kind of image storing. I recommend Picasso
edit: I can give you a sample code for pixel iteration. Feel free to enhance it:
public static void iterateOverImage(BufferedImage image){
int currentColor;
for(int x = 0; x < widthInPx; x++){
for(int y = 0; y < heightInPx; y++){
currentColor = image.getRGB(x, y);
//do something with it (i.e comparison)
}
}
}
edit3:
public int sizeInByte() {
try {
ByteArrayOutputStream b = new ByteArrayOutputStream();
ObjectOutputStream o = new ObjectOutputStream(b);
o.writeObject(yourObject);
o.flush();
o.close();
return b.length;
} catch (IOException e) {
ExceptionHandler.logAndSendException(e);
}
return -1;
}
And cgew85 answer!
Sorry for my english and greets, Steve.