-1

我正在开发一个从服务器获取图像 url 的应用程序。我必须使用该网址并设置图像背景。每件事都在 15-20 分钟内运行良好。该应用程序崩溃后。

03-06 18:35:43.871: E/GraphicsJNI(22189): VM 不允许我们分配 93000 字节

发生此错误时,它会在 logcat 中显示此行。有什么解决办法吗?

4

3 回答 3

1

在你使用你Bitmap的 s 之后,你应该使用recycle()它们。此外,将所有位图包装到WeakReferences 中,让垃圾收集器更轻松地释放资源。

Android 应用程序有一个非常严格的内存限制,当使用Bitmaps 进行体操时很容易达到这个限制。

于 2013-03-06T13:34:05.550 回答
1

发生这种情况的原因是您将巨大的图片设置为您ImageView的设备,直到您的设备内存不足。

您应该做的是创建此图片的缩略图版本并将它们应用到ImageView. ImageView并仅在单击或根本不显示时才显示完整图像。

您可以使用此方法从文件中获取图像的缩略图,但您必须先将设备上的图像保存到文件对象:

public static Bitmap decodeSampledBitmapFromFile(String path,
        int reqWidth, int reqHeight) { // BEST QUALITY MATCH

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        int inSampleSize = 1;

        if (height > reqHeight) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        }

        int expectedWidth = width / inSampleSize;

        if (expectedWidth > reqWidth) {
            //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }


    options.inSampleSize = inSampleSize;

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeFile(path, options);
}
于 2013-03-06T13:35:25.790 回答
0

您应该确保正确处理并且不使用太大或留在内存中的图像。请参阅 android 的此文档

http://developer.android.com/reference/android/app/Application.html#onLowMemory%28%29

您可以覆盖 onLowMemory() 方法来管理您的内存使用情况。

于 2013-03-06T15:15:34.653 回答