报错如下:
07-04 11:21:27.951: ERROR/AndroidRuntime(1140): FATAL EXCEPTION: main
07-04 11:21:27.951: ERROR/AndroidRuntime(1140): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
07-04 11:21:27.951: ERROR/AndroidRuntime(1140):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
07-04 11:21:27.951: ERROR/AndroidRuntime(1140):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459)
07-04 11:21:27.951: ERROR/AndroidRuntime(1140):     at com.cooliris.media.UriTexture.createFromUri(UriTexture.java:175)
07-04 11:21:27.951: ERROR/AndroidRuntime(1140):     at com.cooliris.media.CropImage.onCreate(CropImage.java:268)
07-04 11:21:27.951: ERROR/AndroidRuntime(1140):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-04 11:21:27.951: ERROR/AndroidRuntime(1140):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-04 11:21:27.951: ERROR/AndroidRuntime(1140):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-04 11:21:27.951: ERROR/AndroidRuntime(1140):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-04 11:21:27.951: ERROR/AndroidRuntime(1140):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-04 11:21:27.951: ERROR/AndroidRuntime(1140):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-04 11:21:27.951: ERROR/AndroidRuntime(1140):     at android.os.Looper.loop(Looper.java:123)
07-04 11:21:27.951: ERROR/AndroidRuntime(1140):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-04 11:21:27.951: ERROR/AndroidRuntime(1140):     at java.lang.reflect.Method.invokeNative(Native Method)
07-04 11:21:27.951: ERROR/AndroidRuntime(1140):     at java.lang.reflect.Method.invoke(Method.java:521)
07-04 11:21:27.951: ERROR/AndroidRuntime(1140):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
07-04 11:21:27.951: ERROR/AndroidRuntime(1140):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
07-04 11:21:27.951: ERROR/AndroidRuntime(1140):     at dalvik.system.NativeStart.main(Native Method)
07-04 11:21:29.892: INFO/CacheService(1140): Preparing DiskCache for all thumbnails.
07-04 11:21:29.911: INFO/CacheService(1140): DiskCache ready for all thumbnail
在图库中操作200k以上的图片,设为墙纸时报上面的错误,出现的频率挺高,这里的outofmemory,是否可以修改VM分配给
Gallery进程的ram空间大小,个人认为分配给某个进程的是有系统控制的,不可能在应用层上修改吧?虽然我这里可以修改系统的代码,有知道的木有?

解决方案 »

  1.   

    用BitmapFactory解码一张图片时,有时会遇到该错误。这往往是由于图片过大造成的。要想正常使用,则需要分配更少的内存空间来存储。BitmapFactory.Options.inSampleSize设置恰当的inSampleSize可以使BitmapFactory分配更少的空间以消除该错误。inSampleSize的具体含义请参考SDK文档。例如:BitmapFactory.Options opts = new BitmapFactory.Options();opts.inSampleSize = 4;Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);
    设置恰当的inSampleSize是解决该问题的关键之一。BitmapFactory.Options提供了另一个成员inJustDecodeBounds。BitmapFactory.Options opts = new BitmapFactory.Options();opts.inJustDecodeBounds = true;Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);
    设置inJustDecodeBounds为true后,decodeFile并不分配空间,但可计算出原始图片的长度和宽度,即opts.width和opts.height。有了这两个参数,再通过一定的算法,即可得到一个恰当的inSampleSize。详细...
    http://hi.baidu.com/lizhengjun2010/blog/item/e7ad01d5df1e39ca562c845b.html
      

  2.   

    每个应用程序都有自己的虚拟机,可以设置虚拟机的大小
    VMRuntime.setMinimumHeapSize()
      

  3.   

    官网有相应的文章:
    http://developer.android.com/resources/articles/avoiding-memory-leaks.html