10

I am trying to get image from gallery and setting up it on ImageView , Hear is okay well i get and set image on ImageView, but now i want to check image size of selected image in kb so i set the validaion for image uploading. Please anyone can suggest me how to check selected image size less then 100kb or not?, Hear is my code for image selecting and setting it.

Choosing Image useing Intent

 Intent iv = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(iv, RESULT_LOAD_IMAGE);

and get Image Result code ..

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        picturePath = cursor.getString(columnIndex);
        cursor.close();
        Bitmap bmp=BitmapFactory.decodeFile(picturePath);

        ivLogo.setImageBitmap(bmp);
            uploadNewPic();
    }
}
Qutbuddin Bohra
  • 1,165
  • 1
  • 11
  • 29

4 Answers4

19

to know the size is less then 100kb. you should know the image size to compare. there is some method to know the size of bitmap

method 1

 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; 

method 2

 File file = new File("/sdcard/Your_file");
 long length = file.length() / 1024; // Size in KB

For more Study

go for http://developer.android.com/reference/android/graphics/Bitmap.html#getByteCount%28%29

7

Get file size as

File img = new File(picturePath);
int length = img.length();

it will return size in bytes. you can convert byte into kb

Arun Kumar
  • 2,874
  • 1
  • 18
  • 15
0
   ArrayList<String> filePaths = new ArrayList<>();
    ArrayList<String> newFilePath = new ArrayList<>();  //for storing file path which size is less than 100 KB

    if (imagePaths != null) {
        filePaths.addAll(imagePaths);
        for (int i = 0; i < filePaths.size(); i++) {
            File file = new File(filePaths.get(i));
            int file_size = Integer.parseInt(String.valueOf(file.length() / 1024));     //calculate size of image in KB
            if (file_size < 100)
                newFilePath.add(filePaths.get(i));  //if file size less than 100 KB then add to newFilePath ArrayList
        }
    }

Here imagePaths stores path of all images that we have selected. Then if imagePaths is not null then add all images path in filePaths. You can use this code for document type of file also.

Khyati Vara
  • 1,042
  • 13
  • 22
0

Just input URI from the intent and get the size of any file

uri = data.getData();
        Cursor returnCursor = getContentResolver().query(uri, null, null, null, null);
        int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
        int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
        returnCursor.moveToFirst();
        Log.e("TAG", "Name:" + returnCursor.getString(nameIndex));
        Log.e("TAG","Size: "+Long.toString(returnCursor.getLong(sizeIndex)));

It will give size in bytes, So 100kb will be 100000bytes. I think this will help you.

sanchit
  • 111
  • 1
  • 4