1

I have an app which contain a "imageview" and a "button" known as uploadImage. when i click uploadimage it is opening a a chooser option from where user can choose image and set it in imageview. Problem is that before setting image in image view i want to check whether image size is not more that 200 kb if found then show toast message otheriwse proceed further.

code:-

private void showFileChooser() {
    // Create intent to Open Image applications like Gallery, Google Photos
    Intent galleryIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(galleryIntent,PICK_IMAGE_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        // When an Image is picked
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
                && null != data) {
            // Get the Image from data

            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            // Get the cursor
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            // Move to first row
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            imgDecodableString = cursor.getString(columnIndex);
            cursor.close();

            // Set the Image in ImageView after decoding the String
            m_UploadImage.setImageBitmap(BitmapFactory
                    .decodeFile(imgDecodableString));

        } else {
            Toast.makeText(this, "You haven't picked Image",
                    Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                .show();
    }
}
Vishal
  • 67
  • 2
  • 8

3 Answers3

0

You can check the image size using the following Code:

Bitmap bitImage = BitmapFactory.decodeResource(getResources(), 
            R.drawable.ic_launcher);// For example I took ic_launcher


     Bitmap bitmap = bitImage;
     ByteArrayOutputStream stream = new ByteArrayOutputStream();   
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);   
     byte[] imageInByte = stream.toByteArray(); 
     long sizeOfImage = imageInByte.length; //Image size

Your Code :

private void showFileChooser() {
    // Create intent to Open Image applications like Gallery, Google Photos
    Intent galleryIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(galleryIntent,PICK_IMAGE_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        // When an Image is picked
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
                && null != data) {
            // Get the Image from data

            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            // Get the cursor
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            // Move to first row
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            imgDecodableString = cursor.getString(columnIndex);
            cursor.close();

                // Set the Image in ImageView after decoding the String
 Bitmap bitImage = BitmapFactory.decodeFile(imgDecodableString);
Bitmap bitmap = bitImage;
         ByteArrayOutputStream stream = new ByteArrayOutputStream();   
         bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);   
         byte[] imageInByte = stream.toByteArray(); 
             long sizeOfImage = imageInByte.length; //Image size
//Code to check image size greater than 20KB
    if(sizeofImage/1024 > 200){
     Toast.makeText(this, "Image size more than 200KB", Toast.LENGTH_LONG)

    }else{
                m_UploadImage.setImageBitmap(bitImage);

          }  
                } else {
                    Toast.makeText(this, "You haven't picked Image",
                            Toast.LENGTH_LONG).show();
                }
            } catch (Exception e) {
                Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                        .show();
            }
        }
Athul
  • 801
  • 7
  • 16
0

Or you can try this way ,

First you get the Bitmap attached on the ImageView:

using this :

Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();

In your code :

    private void showFileChooser() {
        // Create intent to Open Image applications like Gallery, Google Photos
        Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(galleryIntent,PICK_IMAGE_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            // When an Image is picked
            if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
                    && null != data) {
                // Get the Image from data

                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                // Get the cursor
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                // Move to first row
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                imgDecodableString = cursor.getString(columnIndex);
                cursor.close();

                // Set the Image in ImageView after decoding the String
                m_UploadImage.setImageBitmap(BitmapFactory
                        .decodeFile(imgDecodableString));


               //Get the Bitmap in your ImageView

Bitmap bitmap = ((BitmapDrawable)m_UploadImage.getDrawable()).getBitmap();

               // Then check the Image size 
ByteArrayOutputStream stream = new ByteArrayOutputStream();   
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);   
     byte[] imageInByte = stream.toByteArray(); 
     long lengthbmp = imageInByte.length; 

  Toast.makeText(this, "Length of the Image :" + lengthbmp,
                        Toast.LENGTH_LONG).show();


            } else {
                Toast.makeText(this, "You haven't picked Image",
                        Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                    .show();
        }
    }
Lutaaya Huzaifah Idris
  • 3,596
  • 8
  • 38
  • 77
0

Better you create a an another class and write this public method in that class because in future you can use this class.

 public class UtilClassName{
 public static int getFileSize(Uri imageUri,Activity activity){
     int kb_size=0;
     try {
        InputStream is=activity.getContentResolver().openInputStream(imageUri);
        int byte_size=is.available();
        int kb_size=byte_size/1024;
    }
    catch (Exception e){
       // here you can handle exception here
    }
    return kb_size;
}
}

In your code use this logic

 if(UtilClassName.getFileSize(selectedImage,this)<=200){
    Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        // Move to first row
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        imgDecodableString = cursor.getString(columnIndex);
        cursor.close();

        // Set the Image in ImageView after decoding the String
        m_UploadImage.setImageBitmap(BitmapFactory
                .decodeFile(imgDecodableString));
 }
 else{
   //show a warning to the user
 }

Try this method and if you are facing any issue let me know. I had faced this problem before and i created this method for a file compressor class. I hope you will get solution.

Vishnu KR
  • 718
  • 1
  • 8
  • 22