从Android的画廊选择的图像显示内存错误 [英] Android choose image from gallery showing memory error

查看:159
本文介绍了从Android的画廊选择的图像显示内存错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在code样品的工作,我不得不从图库中选择图片的code是工作,但由画廊图像的选择之后,我得到的的OutOfMemoryError 在我的的onActivityResult

我能够得到的小图像,但大的图像就创设问题。

下面是我的code:

  {尝试
                    乌里selectedImageUri = data.getData();
                    的String [] = filePathColumn {MediaStore.Images.Media.DATA};
                    光标光标= getContentResolver()查询(selectedImageUri,filePathColumn,NULL,NULL,NULL);
                    cursor.moveToFirst();
                    INT参数:columnIndex = cursor.getColumnIndex(filePathColumn [0]);
                    字符串文件路径= cursor.getString(参数:columnIndex);
                    cursor.close();
                    位= BitmapFactory.de codeFILE(文件路径);
                    _profileImage.setImageBitmap(位图);
                    _profileImage.setScaleType(ScaleType.FIT_XY);
                    Constant._addPhotoBitmap =位图;
                    ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
                    位图resizedbitmap = Bitmap.createScaledBitmap(位图,200,200,真);
                    resizedbitmap.com preSS(Bitmap.Com pressFormat.PNG,100,BAOS);
                    字节[] = _byteArray baos.toByteArray();
                    字符串的base64 = Base64.en codeToString(_byteArray,Base64.DEFAULT);
                    Constant._addPhotoBase64 = BASE64;
                }赶上(OutOfMemoryError异常五){
                    e.printStackTrace();
                    Constant.showAlertDialog(Constant.errorTitle,
                            影像尺寸过large.Please上传小图片。
                            DriverProfileScreen.this,FALSE);
                }赶上(例外五){
                    e.printStackTrace();
                }


解决方案

您deirectly基于其URI path..thats为什么它被扔exception..before加载图像设置一些options..this会降低解码文件内存映像loading..Use任何你想要的大小此方法用于加载图像。

  / **
 *返回从给定的URL缩略图位图
 *
 * @参数路径
 * @参数thumbnailSize
 * @返回
 * /
私人位图getThumbnailBitmap(最后字符串的路径,最终诠释thumbnailSize){
    位图位图;
    BitmapFactory.Options边界=新BitmapFactory.Options();
    bounds.inJustDe codeBounds = TRUE;
    BitmapFactory.de codeFILE(路径,边界);
    如果((bounds.outWidth == -1)||(bounds.outHeight == -1)){
        位= NULL;
    }
    INT originalSize =(bounds.outHeight> bounds.outWidth)? bounds.outHeight
            :bounds.outWidth;
    BitmapFactory.Options选择采用=新BitmapFactory.Options();
    opts.inSampleSize = originalSize / thumbnailSize;
    位= BitmapFactory.de codeFILE(路径,选择采用);
    返回位图;
}

I am working on a code sample where I have to choose an image from gallery the code is working but after selection of image from gallery I get OutOfMemoryError in my OnActivityResult

I am able to get small images but large images are creating problem.

Here is my code:

try{
                    Uri selectedImageUri = data.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getContentResolver().query(selectedImageUri, filePathColumn, null, null, null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String filePath = cursor.getString(columnIndex);
                    cursor.close();
                    bitmap = BitmapFactory.decodeFile(filePath);
                    _profileImage.setImageBitmap(bitmap);
                    _profileImage.setScaleType(ScaleType.FIT_XY);
                    Constant._addPhotoBitmap=bitmap;
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    Bitmap resizedbitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
                    resizedbitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
                    byte [] _byteArray = baos.toByteArray();
                    String base64 = Base64.encodeToString(_byteArray,Base64.DEFAULT);
                    Constant._addPhotoBase64 = base64;
                }catch (OutOfMemoryError e) {
                    e.printStackTrace();
                    Constant.showAlertDialog(Constant.errorTitle,
                            "Image size is too large.Please upload small image.",
                            DriverProfileScreen.this, false);
                }   catch (Exception e) {
                    e.printStackTrace();
                }

解决方案

You are deirectly decoding the file based on its uri path..thats why it is throwing exception..before loading image set some options..this will reduce the memory for the image loading..Use this method for loading image whatever size you want..

/**
 * returns the thumbnail image bitmap from the given url
 * 
 * @param path
 * @param thumbnailSize
 * @return
 */
private Bitmap getThumbnailBitmap(final String path, final int thumbnailSize) {
    Bitmap bitmap;
    BitmapFactory.Options bounds = new BitmapFactory.Options();
    bounds.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, bounds);
    if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) {
        bitmap = null;
    }
    int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
            : bounds.outWidth;
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = originalSize / thumbnailSize;
    bitmap = BitmapFactory.decodeFile(path, opts);
    return bitmap;
}

这篇关于从Android的画廊选择的图像显示内存错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆