簡體   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