【问题标题】:How do I check the size of a picture a user selected from image gallery?如何检查用户从图库中选择的图片的大小?
【发布时间】:2021-01-04 05:40:56
【问题描述】:

在我的 Android 应用中,我允许用户从他们的图库中选择一张图片作为他们的个人资料图片。

这是从他们的图库中选择图片的代码:

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

这是我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)
    }
}

这是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()
        }
}

如何提醒用户他们选择的图片太大?在调用uploadProfileImage 之前如何确定图像的大小?或者 Firebase 存储中是否有办法防止上传尺寸过大的图像?我希望最大照片大小为 2 MB。

【问题讨论】:

标签: android firebase-storage android-bitmap android-image android-gallery


【解决方案1】:
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

创建一个bitmap然后就可以得到高度和宽度

【讨论】:

  • 这是获取宽度和高度,我的问题是关于获取文件图像大小。
  • File file = new File(selectedPath); int file_size = Integer.parseInt(String.valueOf(file.length()/1024));
  • my question is about getting the file image size. – 你从来没有提到文件大小。您只提到了图像大小。您的函数会上传位图。不是文件。 @Tom Darious
  • @blackapps 我在帖子中指定了 2 MB。这怎么没有让你清楚我正在寻找文件大小?
  • @faysalneowaz 您的方法由于某种原因总是返回 0。我用profileImageUri.path替换了selectedPath
【解决方案2】:

我在我的项目中做了这件事,我们得到了图像文件媒体类的大小

          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();
            }

在这段代码中,我们获取任何文件大小并根据文件大小设置条件... 我希望这段代码对你有用

【讨论】:

    【解决方案3】:

    你可以向上下文的内容解析器请求这个:

    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")
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多