0

I'm trying to take a screenshot of one layout in my app. but when I press screenshot button, App crashes.

Log:

05-05 23:17:29.000: E/AndroidRuntime(21992): FATAL EXCEPTION: main
05-05 23:17:29.000: E/AndroidRuntime(21992): java.lang.NullPointerException
05-05 23:17:29.000: E/AndroidRuntime(21992):    at android.graphics.Bitmap.createBitmap(Bitmap.java:526)
05-05 23:17:29.000: E/AndroidRuntime(21992):    at com.tiktak.babyalbum.Helper$8.onClick(Helper.java:298)
05-05 23:17:29.000: E/AndroidRuntime(21992):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:185)
05-05 23:17:29.000: E/AndroidRuntime(21992):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 23:17:29.000: E/AndroidRuntime(21992):    at android.os.Looper.loop(Looper.java:137)
05-05 23:17:29.000: E/AndroidRuntime(21992):    at android.app.ActivityThread.main(ActivityThread.java:5306)
05-05 23:17:29.000: E/AndroidRuntime(21992):    at java.lang.reflect.Method.invokeNative(Native Method)
05-05 23:17:29.000: E/AndroidRuntime(21992):    at java.lang.reflect.Method.invoke(Method.java:511)
05-05 23:17:29.000: E/AndroidRuntime(21992):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
05-05 23:17:29.000: E/AndroidRuntime(21992):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
05-05 23:17:29.000: E/AndroidRuntime(21992):    at dalvik.system.NativeStart.main(Native Method)

I'm using a Helper class for having a cleaner code, and this the Helper Code Arround Line 298:

                View u = act.findViewById(id);
                u.setDrawingCacheEnabled(true);                                                
                LinearLayout z = (LinearLayout) act.findViewById(id);
                int totalHeight = z.getHeight();
                int totalWidth = z.getWidth();
                u.layout(0, 0, totalWidth, totalHeight);    
                u.buildDrawingCache(true);
298-->          Bitmap b = Bitmap.createBitmap(u.getDrawingCache()); 
                u.setDrawingCacheEnabled(false);
                String filePath = Environment.getExternalStorageDirectory()
                        + File.separator + st1;
                File myPath = new File(filePath);
                FileOutputStream fos = null;

It's a weird situation, Because if I test it on a high level cell phone (Like Samsung s4) it crashes, But if I test it on a low level cell phone (Like HTC wildfire s) it works just fine.

any idea?

Edit: Based on Goran comment I checked to see what is returning null in this way:

                View u = act.findViewById(id);               
                LinearLayout z = (LinearLayout) act.findViewById(id);
                u.setDrawingCacheEnabled(true);
                int totalHeight = z.getHeight();
                int totalWidth = z.getWidth();
                u.layout(0, 0, totalWidth, totalHeight);    
                u.buildDrawingCache(true);
                if (u.getDrawingCache() == null) {
                    Toast.makeText(act.getApplicationContext(), "DrawingCache is Null",
                            Toast.LENGTH_LONG).show();
                } else {
                    Bitmap b = Bitmap.createBitmap(u.getDrawingCache()); 
                    u.setDrawingCacheEnabled(false);
                    String filePath = Environment.getExternalStorageDirectory()
                            + File.separator + st1;
                    File myPath = new File(filePath);

and in Wildfire S, It worked, but in s4 I got "DrawingCatch is null" Toast.

So how to prevent getting null?

SaDeGH_F
  • 471
  • 1
  • 3
  • 14

1 Answers1

0

Thanks to this answer I solved my problem By using this Code:

CustomView view = (CustomView) findViewById(R.id.customViewId);
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), 
view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); 
Canvas bitmapHolder = new Canvas(bitmap); 
view.draw(bitmapHolder);
Community
  • 1
  • 1
SaDeGH_F
  • 471
  • 1
  • 3
  • 14