java 如何检查图像大小小于 100kb android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29137003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to check image size less then 100kb android
提问by Qutbuddin Bohra
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.
我正在尝试从图库获取图像并进行设置ImageView
,听说没问题,我得到并设置了图像ImageView
,但现在我想检查所选图像的图像大小,kb
因此我设置了图像上传的验证。请任何人都可以建议我如何检查选定的图像尺寸是否小于100kb
?,听听我的图像选择和设置代码。
Choosing Image useing Intent
选择图像使用 Intent
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();
}
}
回答by Sukhwant Singh Grewal
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
要知道大小小于 100kb。您应该知道要比较的图像大小。有一些方法可以知道位图的大小
method 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;
method 2
方法二
File file = new File("/sdcard/Your_file");
long length = file.length() / 1024; // Size in KB
For more Study
更多研究
go for http://developer.android.com/reference/android/graphics/Bitmap.html#getByteCount%28%29
去http://developer.android.com/reference/android/graphics/Bitmap.html#getByteCount%28%29
回答by Arun Kumar
Get file size as
获取文件大小为
File img = new File(picturePath);
int length = img.length();
it will return size in bytes. you can convert byte into kb
它将以字节为单位返回大小。您可以将字节转换为 kb
回答by Khyati Fatania
ArrayList<String> filePaths = new ArrayList<>();
ArrayList<String> newFilePath = new ArrayList<>(); //for storing file path which size is less than 100 KB
if (imagePaths != null) {
filePaths.addAll(imagePaths);
for (int i = 0; i < filePaths.size(); i++) {
File file = new File(filePaths.get(i));
int file_size = Integer.parseInt(String.valueOf(file.length() / 1024)); //calculate size of image in KB
if (file_size < 100)
newFilePath.add(filePaths.get(i)); //if file size less than 100 KB then add to newFilePath ArrayList
}
}
Here imagePaths
stores path of all images that we have selected. Then if imagePaths
is not null then add all images path in filePaths
. You can use this code for document type of file also.
这里imagePaths
存储我们选择的所有图像的路径。然后如果imagePaths
不为空,则将所有图像路径添加到filePaths
. 您也可以将此代码用于文件的文档类型。
回答by sanchit
Just input URI from the intent and get the size of any file
只需从意图输入 URI 并获取任何文件的大小
uri = data.getData();
Cursor returnCursor = getContentResolver().query(uri, null, null, null, null);
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
returnCursor.moveToFirst();
Log.e("TAG", "Name:" + returnCursor.getString(nameIndex));
Log.e("TAG","Size: "+Long.toString(returnCursor.getLong(sizeIndex)));
It will give size in bytes, So 100kb will be 100000bytes. I think this will help you.
它将以字节为单位给出大小,因此 100kb 将是 100000 字节。我想这会对你有所帮助。