繁体   English   中英

无法拍摄布局的屏幕截图

[英]can't take screenshot of a layout

我正在尝试对我的应用程序中的一种布局进行截图。 但是当我按屏幕截图按钮时,App崩溃了。

日志:

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)

我正在使用一个Helper类来获得更简洁的代码,这是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;

这是一个奇怪的情况,因为如果我在高水平的手机(如Samsung s4)上测试它会崩溃,但是如果我在低水平的手机(如HTC wildfire s)上测试它就可以了。

任何想法?

编辑:基于Goran注释,我检查了一下以这种方式返回null:

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

在Wildfire S中,它起作用了,但是在s4中,我得到了“ DrawingCatch为null” Toast。

那么如何防止空值呢?

多亏了这个答案,我通过使用以下代码解决了我的问题:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM