0

In my Android app, I allow the user to select a picture from their gallery for their profile picture.

This is the code for selecting an image from their gallery:

profile_image_view.setOnClickListener {
    val intent = Intent(Intent.ACTION_PICK)
    intent.type = "image/*"
    startActivityForResult(intent, PROFILE_REQUEST_CODE)
}

This is the code for my onActivityResult:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    // Result of our profile image change
    if (requestCode == PROFILE_REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) {
        // Proceed and check what the selected image was
        profileImageUri = data.data!!

        // Get the bitmap of the image
        if (Build.VERSION.SDK_INT < 28) {
            profileBitmap =
                MediaStore.Images.Media.getBitmap(contentResolver, profileImageUri)
        } else {
            val source =
                ImageDecoder.createSource(contentResolver, profileImageUri)
            profileBitmap = ImageDecoder.decodeBitmap(source)
        }

        // Upload the user's profile image to Firebase Storage
        uploadProfileImage(profileBitmap)
    }
}

This is the code for uploadProfileImage:

private fun uploadProfileImage(bitmap: Bitmap) {
    loadingDialog.startLoadingDialog()
    val filename: String = auth.currentUser!!.uid
    val ref: StorageReference = storage.getReference("/images/$filename")
    ref.putFile(profileImageUri)
        .addOnSuccessListener {
            ref.downloadUrl.addOnSuccessListener {
                userInfo.child("profileImageUrl").setValue(it.toString())
                profile_image_view.setImageBitmap(bitmap)
                loadingDialog.dismissDialog()
            }
        }
        .addOnFailureListener {
            loadingDialog.dismissDialog()
            Toast.makeText(
                applicationContext, "${it.message}",
                Toast.LENGTH_LONG
            ).show()
        }
}

How do I alert the user that the image they have picked is too big? How do I figure out the size of the image before calling uploadProfileImage? Or is there a way in Firebase Storage to prevent images from being uploaded where the size is too big? I want the maximum photo size to be 2 MB.

Tom Darious
  • 434
  • 6
  • 18

3 Answers3

0
Drawable drawable = ImageView.getDrawable();
//you should call after the bitmap drawn
Rect bounds = drawable.getBounds();
int width = bounds.width();
int height = bounds.height();
int bitmapWidth = drawable.getIntrinsicWidth(); //this is the bitmap's width
int bitmapHeight = drawable.getIntrinsicHeight(); //this is the bitmap's height

Create a bitmap and then you can get the height and width

faysal neowaz
  • 343
  • 1
  • 7
  • This is getting the width and height, my question is about getting the file image size. – Tom Darious Jan 04 '21 at 06:14
  • `File file = new File(selectedPath); int file_size = Integer.parseInt(String.valueOf(file.length()/1024));` – faysal neowaz Jan 04 '21 at 06:48
  • `my question is about getting the file image size. –` You have never mentioned file size. You only mentioned image size. And your function uploads a bitmap. Not a file. @Tom Darious – blackapps Jan 04 '21 at 09:17
  • @blackapps I specified 2 MB in my post. How did that not make it clear for you I'm looking for the file size? – Tom Darious Jan 05 '21 at 01:21
  • @faysalneowaz Your method always return 0 for some reason. I replaced `selectedPath` with `profileImageUri.path`. – Tom Darious Jan 05 '21 at 03:45
0

i did this thing in my project we get size of images File Media class

          java.net.URI juri = null;
            try {
                juri = new java.net.URI(uri.toString());
                File mediaFile = new File(juri.getPath());
                long fileSizeInBytes = mediaFile.length();
                long fileSizeInKB = fileSizeInBytes / 1024;
                long fileSizeInMB = fileSizeInKB / 1024;

                if (fileSizeInMB > 10) {
                    Toast.makeText(this,"Video files lesser than 5MB are allowed",Toast.LENGTH_LONG).show();
                    return;
                }
                Toast.makeText(this, mediaFile.toString(), Toast.LENGTH_SHORT).show();
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }

In this code we get any file size and make condition on file size... i hope this code is useful for you

0

You can request this to the context's content resolver:

fun getFileSize(context: Context, uri: Uri): Long {
    val projection = arrayOf(OpenableColumns.SIZE)
    val cursor = context.contentResolver.query(uri, projection, null, null, null)
    cursor?.use {
        it.moveToFirst()
        val sizeColumnId = it.getColumnIndexOrThrow(OpenableColumns.SIZE)
        return it.getLong(sizeColumnId)
    } ?: throw IllegalStateException("The information can't be resolved, probably the file doesn't exist")
}
crgarridos
  • 8,758
  • 3
  • 49
  • 61