问题描述
我正在尝试从图库中获取图像并在 ImageView
上设置它,听到我很好,我在 ImageView上设置图像
,但现在我想检查 kb
中所选图像的图像大小,所以我设置了图像上传的验证。
请有人建议我如何检查所选图像尺寸是否小于 100kb
或不??
听到我的图像选择和设置它的代码。
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.
选择图像使用意图
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();
}
}
推荐答案
到知道尺寸小于100kb。你应该知道要比较的图像大小。有一些方法可以知道位图的大小
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
方法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;
方法2
File file = new File("/sdcard/Your_file");
long length = file.length() / 1024; // Size in KB
更多研究
转到
这篇关于如何检查图像大小小于100kb android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!