【发布时间】:2014-01-15 22:02:33
【问题描述】:
BitmapFactory.decodeFile 仅当我从手机图库中选择图像时才会导致 outOfMemoryError,而不是当我从使用相机意图拍摄照片中选择图像时。在使用相机意图时,我实际上避免使用 decodeFile。从图库中选择(从意图中获取数据)时,我可以使用相同的方法吗?这是我以两种方式获取图像的代码:
如果图片是从设备的图库中选择的:
if (requestCode == 1)
{
if (intent != null && resultcode == RESULT_OK)
{
ImageHelper ih = new ImageHelper();
mProfilePicPath = ih.getSelectedImageFilePathFromGallery(this, intent);
Bitmap portraitPhoto = ih.getChosenImageFromGallery(mProfilePicPath);
try{
ih.saveImage("Profile pics", portraitPhoto, this);
ImageHelper.getChosenImageFromGallery:
public Bitmap getChosenImageFromGallery(String imagePath) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imagePath, options);
options.inSampleSize = DressingRoomActivity.calculateInSampleSize(options, 500, 500);
options.inJustDecodeBounds = false;
Bitmap portraitPhoto = ImageHelper.convertToPortraitOrientation(options, imagePath);
return portraitPhoto;
}
如果图像是通过使用相机拍照选择的:
else if ((requestCode == CAMERA_REQUEST)&& (resultcode == Activity.RESULT_OK)){
Bundle extras = intent.getExtras();
Uri selectedImage = intent.getData();
if (extras != null) {
ImageHelper ih = new ImageHelper();
mProfilePicPath = ih.getFilePathFromCameraPhoto(this, selectedImage);
Bitmap photo = (Bitmap) intent.getExtras().get("data");
try{
ih.saveImage("Profile pics", photo, this);
如您所见,要从图库中获取图像并从中获取位图,我必须使用此代码 options.inSampleSize = DressingRoomActivity.calculateInSampleSize(options, 500, 500); 创建仅 500 x 500 像素的位图,否则会导致行上出现 outOfMemoryError @ 987654325@ 在ImageHelper.convertToPortraitOrientation(options, imagePath); 方法中。这意味着用户从相机中获得其图像的原样质量,并从他们在图库中的图像中获得 500 像素的质量。
我的问题:1) 从图库中选择时,有什么方法可以不让我的图像质量达到 500 像素?就像以某种方式避免这条线:Bitmap bmp = BitmapFactory.decodeFile(path, options); 或其他方式?
2) 对于画廊,我不得不将其转换为肖像,这只是一个小麻烦,但这是为什么呢?如果我不这样做,它会侧向翻转照片。
【问题讨论】:
标签: android image bitmap out-of-memory