0

我正在使用以下代码Bitmap从 a 获取 a View

private static Bitmap loadBitmapFromView(View yourView) {
        Bitmap snapshot = null;
        Drawable drawable = null;
        yourView.setDrawingCacheEnabled(true);
        yourView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); //Quality of the snpashot
        try {
            snapshot = Bitmap.createBitmap(yourView.getDrawingCache());
            drawable = new BitmapDrawable(snapshot);
        } finally {
            yourView.setDrawingCacheEnabled(false);
        }
        return snapshot;
    }

但我正在NullPointerException以下行:

snapshot = Bitmap.createBitmap(yourView.getDrawingCache());

这是Logcat

09-19 15:39:15.481: E/AndroidRuntime(8704): FATAL EXCEPTION: main
09-19 15:39:15.481: E/AndroidRuntime(8704): java.lang.NullPointerException
09-19 15:39:15.481: E/AndroidRuntime(8704):     at android.graphics.Bitmap.createBitmap(Bitmap.java:455)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at com.scb.bonuspartner.utils.MapUtility.loadBitmapFromView(MapUtility.java:85)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at com.scb.bonuspartner.utils.MapUtility.createMarker(MapUtility.java:74)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at com.scb.bonuspartner.offeractivity.fragments.MapOfferFragment.onLocationChanged(MapOfferFragment.java:70)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:227)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:160)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:176)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at android.os.Looper.loop(Looper.java:154)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at android.app.ActivityThread.main(ActivityThread.java:4624)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at java.lang.reflect.Method.invokeNative(Native Method)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at java.lang.reflect.Method.invoke(Method.java:511)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

3

试试这个方法

private static Bitmap loadBitmapFromView(View yourView) {
        Bitmap snapshot = null;
        Drawable drawable = null;
        yourView.setDrawingCacheEnabled(true);
        yourView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); //Quality of the snpashot
        try {
            yourView.buildDrawingCache(); // UPDATE HERE
            snapshot = Bitmap.createBitmap(yourView.getDrawingCache());
            drawable = new BitmapDrawable(snapshot);
        } finally {
            yourView.setDrawingCacheEnabled(false);
        }
        return snapshot;
    }

解释 :

因为知识位图是最后一课。因此,垃圾收集器(GC)的优先级非常低,而且内存非常昂贵。默认情况下,视图不会从画布创建位图,因为如果这样做,将为每个膨胀的视图在内存中生成一个位图,这可能会导致构建绘图缓存。因此buildDrawingCache(),给出了一种方法,您可以在需要时生成位图。

于 2013-09-19T10:08:05.497 回答
0

代码应该是

snapshot = snapshot.createBitmap(yourView.getDrawingCache());

因为您已经将快照作为位图投射在那里

Bitmap snapshot = null;

?

于 2013-09-19T10:05:03.263 回答