getDrawable()上的内存不足错误

时间:2016-01-29 08:12:14

标签: android bitmap wallpaper

我希望将当前图像设置为Android设备中的壁纸,并在缩放后将其设置为列表中的图像视图。我正在做的是这个:

public static Bitmap drawableToBitmap (Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable)drawable).getBitmap();
    }

    int width = drawable.getIntrinsicWidth();
    width = width > 0 ? width : 1;
    int height = drawable.getIntrinsicHeight();
    height = height > 0 ? height : 1;

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

drawableToBitmap函数如下:

01-29 13:30:50.150: E/dalvikvm-heap(3281): Out of memory on a 3891216-byte allocation.
    01-29 13:30:50.160: I/dalvikvm(3281): "main" prio=5 tid=1 RUNNABLE
    01-29 13:30:50.160: I/dalvikvm(3281):   | group="main" sCount=0 dsCount=0 obj=0x409c0460 self=0x12810
    01-29 13:30:50.170: I/dalvikvm(3281):   | sysTid=3281 nice=0 sched=0/0 cgrp=default handle=1074082952
    01-29 13:30:50.180: I/dalvikvm(3281):   | schedstat=( 1725234737 955825527 142 ) utm=152 stm=20 core=0
    01-29 13:30:50.190: I/dalvikvm(3281):   at android.graphics.Bitmap.nativeCreate(Native Method)
    01-29 13:30:50.190: I/dalvikvm(3281):   at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
    01-29 13:30:50.190: I/dalvikvm(3281):   at android.graphics.Bitmap.createBitmap(Bitmap.java:585)
    01-29 13:30:50.190: I/dalvikvm(3281):   at android.app.WallpaperManager.generateBitmap(WallpaperManager.java:729)
    01-29 13:30:50.190: I/dalvikvm(3281):   at android.app.WallpaperManager$Globals.getCurrentWallpaperLocked(WallpaperManager.java:265)
    01-29 13:30:50.200: I/dalvikvm(3281):   at android.app.WallpaperManager$Globals.peekWallpaperBitmap(WallpaperManager.java:230)
    01-29 13:30:50.200: I/dalvikvm(3281):   at android.app.WallpaperManager.getDrawable(WallpaperManager.java:350)
    01-29 13:30:50.210: I/dalvikvm(3281):   at storeapps.com.profilechanger.ListActivity.onCreate(ListActivity.java:64)
    01-29 13:30:50.210: I/dalvikvm(3281):   at android.app.Activity.performCreate(Activity.java:4466)
    01-29 13:30:50.220: I/dalvikvm(3281):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    01-29 13:30:50.220: I/dalvikvm(3281):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
    01-29 13:30:50.220: I/dalvikvm(3281):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
    01-29 13:30:50.220: I/dalvikvm(3281):   at android.app.ActivityThread.access$600(ActivityThread.java:123)
    01-29 13:30:50.220: I/dalvikvm(3281):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
    01-29 13:30:50.220: I/dalvikvm(3281):   at android.os.Handler.dispatchMessage(Handler.java:99)
    01-29 13:30:50.220: I/dalvikvm(3281):   at android.os.Looper.loop(Looper.java:137)
    01-29 13:30:50.230: I/dalvikvm(3281):   at android.app.ActivityThread.main(ActivityThread.java:4424)
    01-29 13:30:50.230: I/dalvikvm(3281):   at java.lang.reflect.Method.invokeNative(Native Method)
    01-29 13:30:50.230: I/dalvikvm(3281):   at java.lang.reflect.Method.invoke(Method.java:511)
    01-29 13:30:50.230: I/dalvikvm(3281):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    01-29 13:30:50.230: I/dalvikvm(3281):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    01-29 13:30:50.230: I/dalvikvm(3281):   at dalvik.system.NativeStart.main(Native Method)
    01-29 13:30:50.270: I/Process(570): Sending signal. PID: 3281 SIG: 3
    01-29 13:30:50.270: I/dalvikvm(3281): threadid=3: reacting to signal 3
    01-29 13:30:50.310: W/WallpaperManager(3281): Can't generate default bitmap
    01-29 13:30:50.310: W/WallpaperManager(3281): java.lang.OutOfMemoryError
    01-29 13:30:50.310: W/WallpaperManager(3281):   at android.graphics.Bitmap.nativeCreate(Native Method)
    01-29 13:30:50.310: W/WallpaperManager(3281):   at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
    01-29 13:30:50.310: W/WallpaperManager(3281):   at android.graphics.Bitmap.createBitmap(Bitmap.java:585)
    01-29 13:30:50.310: W/WallpaperManager(3281):   at android.app.WallpaperManager.generateBitmap(WallpaperManager.java:729)
    01-29 13:30:50.310: W/WallpaperManager(3281):   at android.app.WallpaperManager$Globals.getCurrentWallpaperLocked(WallpaperManager.java:265)
    01-29 13:30:50.310: W/WallpaperManager(3281):   at android.app.WallpaperManager$Globals.peekWallpaperBitmap(WallpaperManager.java:230)
    01-29 13:30:50.310: W/WallpaperManager(3281):   at android.app.WallpaperManager.getDrawable(WallpaperManager.java:350)
    01-29 13:30:50.310: W/WallpaperManager(3281):   at storeapps.com.profilechanger.ListActivity.onCreate(ListActivity.java:64)
    01-29 13:30:50.310: W/WallpaperManager(3281):   at android.app.Activity.performCreate(Activity.java:4466)
    01-29 13:30:50.310: W/WallpaperManager(3281):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    01-29 13:30:50.310: W/WallpaperManager(3281):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
    01-29 13:30:50.310: W/WallpaperManager(3281):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
    01-29 13:30:50.310: W/WallpaperManager(3281):   at android.app.ActivityThread.access$600(ActivityThread.java:123)
    01-29 13:30:50.310: W/WallpaperManager(3281):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
    01-29 13:30:50.310: W/WallpaperManager(3281):   at android.os.Handler.dispatchMessage(Handler.java:99)
    01-29 13:30:50.310: W/WallpaperManager(3281):   at android.os.Looper.loop(Looper.java:137)
    01-29 13:30:50.310: W/WallpaperManager(3281):   at android.app.ActivityThread.main(ActivityThread.java:4424)
    01-29 13:30:50.310: W/WallpaperManager(3281):   at java.lang.reflect.Method.invokeNative(Native Method)
    01-29 13:30:50.310: W/WallpaperManager(3281):   at java.lang.reflect.Method.invoke(Method.java:511)
    01-29 13:30:50.310: W/WallpaperManager(3281):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    01-29 13:30:50.310: W/WallpaperManager(3281):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    01-29 13:30:50.310: W/WallpaperManager(3281):   at dalvik.system.NativeStart.main(Native Method)
    01-29 13:30:50.310: D/ProfileChangerApp(3281):  wallpaper manager is :android.app.WallpaperManager@41085b88
    01-29 13:30:50.320: D/ProfileChangerApp(3281): image is not null
    01-29 13:30:50.320: I/dalvikvm(3281): Wrote stack traces to '/data/anr/traces.txt'
    01-29 13:30:50.340: D/AndroidRuntime(3281): Shutting down VM
    01-29 13:30:50.371: W/dalvikvm(3281): threadid=1: thread exiting with uncaught exception (group=0x409bf1f8)
    01-29 13:30:50.430: E/AndroidRuntime(3281): FATAL EXCEPTION: main
    01-29 13:30:50.430: E/AndroidRuntime(3281): java.lang.RuntimeException: Unable to start activity ComponentInfo{storeapps.com.profilechanger/storeapps.com.profilechanger.ListActivity}: java.lang.NullPointerException
    01-29 13:30:50.430: E/AndroidRuntime(3281):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
    01-29 13:30:50.430: E/AndroidRuntime(3281):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
    01-29 13:30:50.430: E/AndroidRuntime(3281):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
    01-29 13:30:50.430: E/AndroidRuntime(3281):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
    01-29 13:30:50.430: E/AndroidRuntime(3281):     at android.os.Handler.dispatchMessage(Handler.java:99)
    01-29 13:30:50.430: E/AndroidRuntime(3281):     at android.os.Looper.loop(Looper.java:137)
    01-29 13:30:50.430: E/AndroidRuntime(3281):     at android.app.ActivityThread.main(ActivityThread.java:4424)
    01-29 13:30:50.430: E/AndroidRuntime(3281):     at java.lang.reflect.Method.invokeNative(Native Method)
    01-29 13:30:50.430: E/AndroidRuntime(3281):     at java.lang.reflect.Method.invoke(Method.java:511)
    01-29 13:30:50.430: E/AndroidRuntime(3281):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    01-29 13:30:50.430: E/AndroidRuntime(3281):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    01-29 13:30:50.430: E/AndroidRuntime(3281):     at dalvik.system.NativeStart.main(Native Method)
    01-29 13:30:50.430: E/AndroidRuntime(3281): Caused by: java.lang.NullPointerException
    01-29 13:30:50.430: E/AndroidRuntime(3281):     at storeapps.com.profilechanger.ListActivity.onCreate(ListActivity.java:70)
    01-29 13:30:50.430: E/AndroidRuntime(3281):     at android.app.Activity.performCreate(Activity.java:4466)
    01-29 13:30:50.430: E/AndroidRuntime(3281):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    01-29 13:30:50.430: E/AndroidRuntime(3281):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
    01-29 13:30:50.430: E/AndroidRuntime(3281):     ... 11 more

但是我收到了这个错误:

find -L [your searched folder] -name [your searched pattern] -exec realpath {} \; >> cscope.files

请帮助我!

5 个答案:

答案 0 :(得分:3)

调整图片大小位图

Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.yourImageName);

以上行将从Bitmap获得Drawable。除了添加上述行以从Bitmap获取Drawable之外,无需执行任何操作。

int height = (bitmap.getHeight() * 512 / bitmap.getWidth());
Bitmap scale = Bitmap.createScaledBitmap(bitmap, 512, height, true);
imageView.setImageBitmap(scale);

答案 1 :(得分:2)

你的Logcat是什么抛出

  

01-29 13:30:50.310:W / WallpaperManager(3281):   java.lang.OutOfMemoryError

在使用可用平台资源无法满足内存请求时抛出此异常。此类请求可以由正在运行的应用程序或 VM或设备的内部功能进行。

  1. 在设置为ImageView之前压缩图像
  2.   

    尽可能从现有位图缩放创建新位图。   如果指定的宽度和高度与当前宽度相同   和源位图的高度,返回源位图,没有   新位图已创建。

    Bitmap.createScaledBitmap(YourImageBitMap, 100, 50, true);
    
    1. 缩小图片尺寸
    2. 您可以阅读 How to deal with the OutOfMemoryError &

      <强> Strange out of memory issue while loading an image to a Bitmap object

答案 2 :(得分:1)

Drawable wallpaper = getApplicationContext().getWallpaper();

wallpaper是参考。

Bitmap.createBitmap可能会使OOM异常。

WindowManager源代码:

// This is the final bitmap we want to return.
try {
Bitmap newbm = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);

答案 3 :(得分:0)

机器人:largeHeap =&#34;真&#34;在您的清单申请标签

答案 4 :(得分:0)

请勿将不是矢量可绘制对象的可绘制对象放入DRAWABLE中,而应放入DRAWABLE-NODPI中。否则,在某些设备上您将获得OOME,因为它们已缩放且具有更高的内存使用率。发生在三星更多