0

I am using the below code for getting a Bitmap from 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;
    }

But I am getting NullPointerException on the following line:

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

Here is the 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)
Pramod Ravikant
  • 1,039
  • 2
  • 11
  • 28

2 Answers2

3

Try this way

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;
    }

Explanation :

For the knowledge Bitmap is final class. So it's priority for garbage collector(GC) is very low and it is very memory expensive.By default view does not creates the bitmap from the canvas because if it do so one bitmap will be generate in memory for each view inflated and it may cause buildDrawingCache. So buildDrawingCache() method is given that you can generate the bitmap when it is required.

Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
0

Should the code be

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

as youve already cast snapshot as Bitmap there

Bitmap snapshot = null;

?

RonnyKnoxville
  • 6,166
  • 10
  • 46
  • 75
  • @JackalopeZero : no. createBitmap is a static method of the Bitmap class. It can be called on an instance, but it is not recommended, especially in this case, since snapshot is null at this point and this will cause another NPE. – njzk2 Sep 19 '13 at 10:14