Qut*_*hra 4 java android image bitmap
我正试图从图库中获取图像并将其设置为开启ImageView,听到没关系我得到并设置图像ImageView,但现在我想检查所选图像的图像大小,kb所以我设置了图像上传的验证.请任何人都可以建议我如何检查选定的图像尺寸是否小于100kb?,听到我的图像选择和设置它的代码.
选择图像使用 Intent
Intent iv = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(iv, RESULT_LOAD_IMAGE);
Run Code Online (Sandbox Code Playgroud)
并获取图像结果代码..
@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();
}
}
Run Code Online (Sandbox Code Playgroud)
小智 13
要知道尺寸小于100kb.你应该知道要比较的图像大小.有一些方法可以知道位图的大小
方法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;
Run Code Online (Sandbox Code Playgroud)
方法2
File file = new File("/sdcard/Your_file");
long length = file.length() / 1024; // Size in KB
Run Code Online (Sandbox Code Playgroud)
更多研究
去http://developer.android.com/reference/android/graphics/Bitmap.html#getByteCount%28%29
获取文件大小为
File img = new File(picturePath);
int length = img.length();
Run Code Online (Sandbox Code Playgroud)
它将以字节为单位返回大小。您可以将字节转换为 kb