2015-08-27 75 views
-1

我試圖從片段中獲取屏幕截圖,但似乎是當我做這件事時 bitmap = view.getDrawingCache();它返回null。有誰知道爲什麼會發生這種情況,我該如何解決這個問題?當採取屏幕截圖時,位圖返回null

這裏是我的代碼:

public void takeScreenshot(View view) { 
     OutputStream output; 

     view = view.getRootView(); 
     view.setDrawingCacheEnabled(true); 
     view.buildDrawingCache(true); 
     File filepath = Environment.getExternalStorageDirectory(); 
     File dir = new File(filepath + "/" 
       + getString(R.string.app_name) + "/"); 
     dir.mkdirs(); 
     File file = new File(dir, getString(R.string.app_name) + ".jpg"); 
     bitmap = view.getDrawingCache(); 

     try { 
      output = new FileOutputStream(file); 
      bitmap.compress(Bitmap.CompressFormat.PNG, 80, output); 
      output.flush(); 
      output.close(); 
      Log.d(TAG, "screenshot saved"); 
     } catch (FileNotFoundException e1) { 
      e1.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

,並調用該方法 takeScreenshot(getActivity().findViewById(R.id.fragment_container));

這裏是logcat的輸出:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.contractfit/com.contractfit.activities.MainActivity}: java.lang.NullPointerException 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471) 
      at android.app.ActivityThread.access$900(ActivityThread.java:175) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:146) 
      at android.app.ActivityThread.main(ActivityThread.java:5602) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
      at com.contractfit.fragments.BaseFragment.takeScreenshot(BaseFragment.java:532) 
      at com.contractfit.fragments.MainFragment.init(MainFragment.java:211) 
      at com.contractfit.fragments.MainFragment.onCreateView(MainFragment.java:116) 
      at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789) 
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955) 
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138) 
      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740) 
      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501) 
      at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551) 
      at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1177) 
      at android.app.Activity.performStart(Activity.java:5461) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2386) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471) 
            at android.app.ActivityThread.access$900(ActivityThread.java:175) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:146) 
            at android.app.ActivityThread.main(ActivityThread.java:5602) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
+0

@ njzk2似乎並不喜歡的,要重複我清楚知道猿是什麼。 – JohanShogun

+0

@JohanShogun我知道,我撤回了近距離投票,沒有看到留言 – njzk2

回答

0

考慮這樣做類似儀器測試的情況下做到這一點,你的代碼很接近,但有點不同。

https://android.googlesource.com/platform/external/droiddriver/+/367267b/src/io/appium/droiddriver/instrumentation/InstrumentationUiDevice.java

獲得root視圖(如果它是公共的):

context.getDriver().getRootElement().getRawElement())); 

採取截圖

public Bitmap call() { 
    Bitmap screenshot; 
    rootView.destroyDrawingCache(); 
    rootView.buildDrawingCache(false); 
    Bitmap drawingCache = rootView.getDrawingCache(); 
    int[] xy = new int[2]; 
    rootView.getLocationOnScreen(xy); 
    if (xy[0] == 0 && xy[1] == 0) { 
    screenshot = Bitmap.createBitmap(drawingCache); 
    } else { 
    Canvas canvas = new Canvas(); 
    Rect rect = new Rect(0, 0, drawingCache.getWidth(), drawingCache.getHeight()); 
    rect.offset(xy[0], xy[1]); 
    screenshot = 
     Bitmap.createBitmap(rect.width() + xy[0], rect.height() + xy[1], Config.ARGB_8888); 
    canvas.setBitmap(screenshot); 
    canvas.drawBitmap(drawingCache, null, new RectF(rect), null); 
    canvas.setBitmap(null); 
    } 
    rootView.destroyDrawingCache(); 
    return screenshot; 
+0

沒有工作,與我有相同的錯誤 –

+0

好吧,如果您刪除了獲取根視圖並僅運行代碼的行,會發生什麼情況你的看法? – JohanShogun