在我的应用程序上从图库(SD 卡)中挑选图像… ¿如何只允许最大 500kb 的照片?
我现在所做的: 我正在使用 android 在我的应用程序上选择图像,并将它们显示在布局的 ImageView 上,有时我会遇到异常 java.lang.OutOfMemoryError:位图大小超出 VM 预算
。当照片大于 500kb 时会发生这种情况。
我需要做什么(而且我不知道该怎么做): 然后,我想做的是允许用户只选择最大 500kb 的照片。如果用户选择高于 500kb 的照片,那么,我的应用程序应该显示一个 toast 告诉用户只能选择最大 500kb 的照片......该怎么做?
这是我的代码:
changeImageButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 1:
{
setResult(1);
finish();
}
case ACTIVITY_SELECT_IMAGE:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
selectedPhoto = BitmapFactory.decodeFile(filePath);
//profileImage.setImageBitmap(selectedPhoto);
profileImage.setImageBitmap(Bitmap.createScaledBitmap(selectedPhoto, 80, 80, false));
}
}
}
profileImage 是我的布局的 ImageView。我使用缩放的 butmap 将图像调整为 80x80
请给我一些帮助来做到这一点,我在谷歌上搜索,但什么也找不到
WHAT I HAVE DONE NOW:
i am picking images on my app with android, and showing them on a ImageView of my layout, and sometimes i got an exception java.lang.OutOfMemoryError: bitmap size exceeds VM budget
. This happens when the photos are higher than 500kb.
WHAT I NEED TO DO (AND I DONT KNOW HOW TO DO IT):
then, what i want to do is to allow the user only to select photos of maximum 500kb. And if the user selects a photo Higher than 500kb, then, my app should show a toast telling the user that only can select photos of max 500kb.... ¿how to do it?
this is my code:
changeImageButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 1:
{
setResult(1);
finish();
}
case ACTIVITY_SELECT_IMAGE:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
selectedPhoto = BitmapFactory.decodeFile(filePath);
//profileImage.setImageBitmap(selectedPhoto);
profileImage.setImageBitmap(Bitmap.createScaledBitmap(selectedPhoto, 80, 80, false));
}
}
}
profileImage is a ImageView of my layout. and i use scaled butmap to resice the image to 80x80
please give me some help to do that, im searching on google and i can't find nothing
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
难道您不能只使用一些标准 Java 来获取文件大小,然后检查您的限制吗?
Can't you just use some standard Java to get the file size, then check against your limit?
为什么你这么讨厌你的用户?您问了错误的问题...您根本不需要限制文件大小。。问题在于解压缩后的图像大小,您可以控制它。
使用采用
BitmapFactory.Options
参数的BitmapFactory.decodeFile()
版本,然后将 options 参数的inSampleSize
字段设置为适当的值(2或4或其他)。您甚至可能想首先使用inJustDecodeBounds
来计算出合理的值。Why do you hate your users so much? You are asking the wrong question... you don't need to cap the file size at all. It's the decompressed image size that's the problem, and you can control that.
Use the version of
BitmapFactory.decodeFile()
which takes aBitmapFactory.Options
parameter, and then set theinSampleSize
field of the options parameter to an appropriate value (2 or 4 or something). You may even want to useinJustDecodeBounds
first to work out a reasonable value.