如何解决如何检查用户从图片库中选择的图片的大小?
在我的 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 Storage 中是否有一种方法可以防止在尺寸太大的情况下上传图像?我希望照片大小上限为 2 MB。
解决方法
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
然后就可以得到高度和宽度
我在我的项目中做了这件事,我们得到了图像文件媒体类的大小
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();
}
在此代码中,我们获取任何文件大小并根据文件大小设置条件... 我希望这段代码对你有用
,您可以向上下文的内容解析器请求此内容:
fun getFileSize(context: Context,uri: Uri): Long {
val projection = arrayOf(OpenableColumns.SIZE)
val cursor = context.contentResolver.query(uri,projection,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")
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。