0

我已经提到了更多关于我的问题。但我还无法解决我的问题,我无法预测为什么它会在特定设备上发生,尤其是 Galaxy S3。我也在其他设备上运行相同的应用程序,它工作正常。我使用 eclipse MAT 发现了内存泄漏。它正好在我的应用程序上使用的图像编辑类上。我没有尝试过 bitmap.recyle(),因为我在整个应用程序中都使用了它。ImageEditView 类用于在屏幕上显示图像。我已经使用下面的代码片段加载了位图。

private Bitmap decodeAndDownsampleImageURI(Uri uri) {
    Bitmap bitmap = null;

    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inPurgeable = true; 

        BufferedInputStream in = new BufferedInputStream(getContext().getContentResolver().openInputStream(uri));
        BitmapFactory.decodeStream(in, null, options);
        in.close();

        int scale = 1;
        if (options.outHeight > IMAGE_MAX_SIZE || options.outWidth > IMAGE_MAX_SIZE) {
            scale = (int) Math.pow(
                    2,
                    (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(options.outHeight, options.outWidth))
                                        / Math.log(0.5)));
        }

        options = new BitmapFactory.Options();
        options.inSampleSize = scale;
        in = new BufferedInputStream(getContext().getContentResolver().openInputStream(uri));
        bitmap = BitmapFactory.decodeStream(in, null, options);
        in.close();
    } catch (FileNotFoundException e) {
        log.error(MyStyleApplication.hockeyAppLogMessage, e);
        Log.e(TAG, "decodeAndDownsampleImageUri()", e);
    } catch (Exception e) {
        log.error(MyStyleApplication.hockeyAppLogMessage, e);
        Log.e(TAG, "decodeAndDownsampleImageUri()", e);
    }

    return bitmap;
}

}

任何人请建议我更好的解决方案来解决我的问题。

4

2 回答 2

1

您可以使用以下代码。它会解决你的问题

BitmapFactory.Options options = new BitmapFactory.Options();
                                options.inSampleSize = 4;
 in = new BufferedInputStream(getContext().getContentResolver().openInputStream(uri));
    bitmap = BitmapFactory.decodeStream(in, null, options);

如果你仍然有问题,那么你可以缩小你的图像。

Bitmap newBmp=Bitmap.createScaledBitmap(bitmap , 100, 100, true);
于 2013-10-14T09:03:42.437 回答
0

您不能将所有位图保存在内存中,这将耗尽内存。您必须将数据写入磁盘缓存。只有当前屏幕或切换屏幕需要内存数据,例如在图库中向左或向右滑动图像。您的应用程序必须由某些事件驱动。因此,当某些事件触发时,有足够的时间从缓存中再次解码位图数据。

于 2013-10-15T02:58:35.003 回答