Android - 如何压缩或缩小图像?
ImageButton avatarButton = (ImageButton) findViewById(R.id.ImageButton_Avatar);
avatarButton.setImageResource(R.drawable.avatar);
strAvatarFilename = "Image.jpg";
final Uri imageUriToSaveCameraImageTo = Uri.fromFile(new File(Environment.getExternalStorageDirectory()+"/Folder/"+strAvatarFilename));
avatarButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent pictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
pictureIntent.putExtra( MediaStore.EXTRA_OUTPUT, imageUriToSaveCameraImageTo );
startActivityForResult(Intent.createChooser(pictureIntent, strAvatarPrompt), TAKE_AVATAR_CAMERA_REQUEST);
Editor editor = Preferences.edit();
editor.putString(PREFERENCES_AVATAR, imageUriToSaveCameraImageTo.getPath());
editor.commit();
}
});
我有这个代码。它拍摄一张照片,然后将其存储在两个位置,SD 卡上的默认位置和我希望将照片存储在的 /folder/ 文件。拍摄后,我使用此刷新屏幕...
ImageButton avatarButton = (ImageButton) findViewById(R.id.ImageButton_Avatar);
String strAvatarUri = Preferences.getString(PREFERENCES_AVATAR, "");
Uri imageUri = Uri.parse(strAvatarUri);
avatarButton.setImageURI(null);
avatarButton.setImageURI(imageUri);
这会刷新图像按钮,以便用户可以看到他们拍摄的图像。然而,它看起来很大并且填满了屏幕。有什么方法可以压缩或缩小图像,以便用户可以以合理的尺寸查看它?谢谢
ImageButton avatarButton = (ImageButton) findViewById(R.id.ImageButton_Avatar);
avatarButton.setImageResource(R.drawable.avatar);
strAvatarFilename = "Image.jpg";
final Uri imageUriToSaveCameraImageTo = Uri.fromFile(new File(Environment.getExternalStorageDirectory()+"/Folder/"+strAvatarFilename));
avatarButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent pictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
pictureIntent.putExtra( MediaStore.EXTRA_OUTPUT, imageUriToSaveCameraImageTo );
startActivityForResult(Intent.createChooser(pictureIntent, strAvatarPrompt), TAKE_AVATAR_CAMERA_REQUEST);
Editor editor = Preferences.edit();
editor.putString(PREFERENCES_AVATAR, imageUriToSaveCameraImageTo.getPath());
editor.commit();
}
});
I have this code. It takes a photo and then stores it in two locations, the default location on the SDcard and the /folder/ file i want the photo to be stored in also. Once taken i refresh the screen using this...
ImageButton avatarButton = (ImageButton) findViewById(R.id.ImageButton_Avatar);
String strAvatarUri = Preferences.getString(PREFERENCES_AVATAR, "");
Uri imageUri = Uri.parse(strAvatarUri);
avatarButton.setImageURI(null);
avatarButton.setImageURI(imageUri);
This refreshes the image button so the user can see the image they have taken. However, this is appears very big and fills the screen. Is there any way i can compress or downsize the image so the user can see it at a reasonable size? thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
其中 createScaledBitmap 的第二个和第三个参数分别是宽度和高度。
http://developer.android.com/reference/android/graphics/Bitmap.html#createScaledBitmap%28android.graphics.Bitmap,%20int,%20int,%20boolean%29
编辑:我现在想到了您可以使用 B
itmapFactory.Options
和inSampleSize
选项以更快、更有效的方式完成此操作:这样您将节省更多内存,并且缩小规模应该更少的时间。
Where the second and third parameters to createScaledBitmap are the width and height respectively.
http://developer.android.com/reference/android/graphics/Bitmap.html#createScaledBitmap%28android.graphics.Bitmap,%20int,%20int,%20boolean%29
EDIT: It now occurred to me that you can do this in a faster, more efficient way, using B
itmapFactory.Options
and theinSampleSize
option:This way you will save even more memory and the downsizing should take less time.
您可以修改 ImageButton 的scaleType 属性(例如fitXY 或cropCenter),并且通过将固定大小设置为LayoutPreferences(高度和宽度),您就可以完成所有设置。
You can mess with the ImageButton's scaleType properties (to fitXY for example, or cropCenter), and by setting a fixed size to LayoutPreferences (height and width) you'll be all set.
Android 库 SiliCompressor 提供此功能,让您在保持图像质量的同时压缩图像。
The Android Library SiliCompressor provide this functionality for you to compress your images while keeping the image quality.