【问题标题】:how to sample the bitmap for memory management in android?如何在 android 中对位图进行采样以进行内存管理?
【发布时间】:2015-07-11 23:54:19
【问题描述】:

我做了一个图像编辑演示,当我将图像传递给另一个活动(增加堆大小)时遇到问题,我的应用程序崩溃了,请告诉我如何直接采样和调整我的位图大小(不是来自资源) 解决内存问题,我的代码如下: 代码

if (Global.photo_editor_bitmap != null) {
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                Global.photo_editor_bitmap.compress(Bitmap.CompressFormat.PNG,
                        100, stream);
                byte[] byteArray = stream.toByteArray();

                i = new Intent(PhotoEditor.this, Enhance.class);
                i.putExtra("bitmap_image", byteArray);
                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            } else {

                Toast.makeText(getApplicationContext(), "No Image", 1).show();
            }

    static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        int inSampleSize = 1; // Default subsampling size
        // See if image raw height and width is bigger than that of required
        // view
        if (options.outHeight > reqHeight || options.outWidth > reqWidth) {
            // bigger
            final int halfHeight = options.outHeight / 2;
            final int halfWidth = options.outWidth / 2;
            // Calculate the largest inSampleSize value that is a power of 2 and
            // keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }
        return inSampleSize;
    }

    static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
            int reqWidth, int reqHeight) {
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);
        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

我得到了decodebitmapFromresources的解决方案,但是我的位图是可用的,所以如何使用这个直接。

【问题讨论】:

    标签: android bitmap out-of-memory image-scaling


    【解决方案1】:

    我有同样的问题,应用程序崩溃并在日志中显示堆大小、分配大小和位图大小的消息,我所做的是检查设备宽度和高度,并在此基础上缩放位图或计算样本尺寸。

    int screenHeight = getResources().getDisplayMetrics().heightPixels;
    int screenWidth = getResources().getDisplayMetrics().widthPixels; 
    

    获取宽度和高度然后放到reqWidth和reqHeight中

    【讨论】:

    • 您好,谢谢您的回复,但我不知道如何在函数中传递我的位图,decodeSampledBitmapFromResource,因为它需要资源并且我已经有位图可用。如何将我的位图传递给它?
    • 如何在 Global.photo_editor_bitmap 中存储位图???因为您将此位图作为 byteArray 发送到另一个活动
    猜你喜欢
    • 2021-02-01
    • 2012-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多