Getting Size of an Image(In Kb or Mb) Selected from Gallery Programatically

Get the exact File Size of an Image from the selected image in gallery

Try this .

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
int dataSize=0;
if (requestCode == 2 && resultCode == RESULT_OK)
{
Uri uri = data.getData();
String scheme = uri.getScheme();
System.out.println("Scheme type " + scheme);
if(scheme.equals(ContentResolver.SCHEME_CONTENT))
{
try {
InputStream fileInputStream=getApplicationContext().getContentResolver().openInputStream(uri);
dataSize = fileInputStream.available();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("File size in bytes"+dataSize);

}
else if(scheme.equals(ContentResolver.SCHEME_FILE))
{
String path = uri.getPath();
try {
f = new File(path);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("File size in bytes"+f.length());
}

}
}

How to get the size of an image in Android?

Thank you for your answers. This is how I finally resolved it:

 Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), 
R.drawable.ic_launcher);


Bitmap bitmap = bitmapOrg;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
long lengthbmp = imageInByte.length;

Appreciate your time and answers :)

Determining image file size + dimensions via Javascript?

Edit:

To get the current in-browser pixel size of a DOM element (in your case IMG elements) excluding the border and margin, you can use the clientWidth and clientHeight properties.

var img = document.getElementById('imageId'); 

var width = img.clientWidth;
var height = img.clientHeight;

Now to get the file size, now I can only think about the fileSize property that Internet Explorer exposes for document and IMG elements...

Edit 2: Something comes to my mind...

To get the size of a file hosted on the server, you could simply make an HEAD HTTP Request using Ajax. This kind of request is used to obtain metainformation about the url implied by the request without transferring any content of it in the response.

At the end of the HTTP Request, we have access to the response HTTP Headers, including the Content-Length which represents the size of the file in bytes.

A basic example using raw XHR:

var xhr = new XMLHttpRequest();
xhr.open('HEAD', 'img/test.jpg', true);
xhr.onreadystatechange = function(){
if ( xhr.readyState == 4 ) {
if ( xhr.status == 200 ) {
alert('Size in bytes: ' + xhr.getResponseHeader('Content-Length'));
} else {
alert('ERROR');
}
}
};
xhr.send(null);

Note: Keep in mind that when you do Ajax requests, you are restricted by the Same origin policy, which allows you to make requests only within the same domain.

Check a working proof of concept here.

Edit 3:

1.) About the Content-Length, I think that a size mismatch could happen for example if the server response is gzipped, you can do some tests to see if this happens on your server.

2.) For get the original dimensions of a image, you could create an IMG element programmatically, for example:

var img = document.createElement('img');

img.onload = function () { alert(img.width + ' x ' + img.height); };

img.src='http://sstatic.net/so/img/logo.png';


Related Topics



Leave a reply



Submit