2016-02-26 49 views
0

我正在開發一個android應用程序,並試圖使下面的代碼有效。我想要做的是:截取整個活動的截圖,包括未顯示的文字(必須向上或向下滾動)。 這是截圖方法:如何在android studio中傳遞整個活動

public static Bitmap takeScreenshot(Activity activity){ 
    View view = activity.getWindow().getDecorView(); 
    view.setDrawingCacheEnabled(true); 
    Bitmap bmap = view.getDrawingCache(); 

    Rect statusBar = new Rect(); 
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(statusBar); 
    Bitmap snapshot = Bitmap.createBitmap(bmap, 0, statusBar.top, bmap.getWidth(), bmap.getHeight() - statusBar.top, null, true); 

    view.setDrawingCacheEnabled(false); 
    return snapshot; 
} 
public void saveBitmap(Bitmap bitmap) { 
    File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png"); 
    FileOutputStream fos; 
    try { 
     fos = new FileOutputStream(imagePath); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
     fos.flush(); 
     fos.close(); 
    } catch (FileNotFoundException e) { 
     Log.e("GREC", e.getMessage(), e); 
    } catch (IOException e) { 
     Log.e("GREC", e.getMessage(), e); 
    } 
} 

我想打電話給takeScreenshot以下功能,但我不知道如何在takeScreenshot的參數傳遞的活動。我試圖複製活動的名稱,但它不起作用。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_detailed__info); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    Intent intent = getIntent(); 

    id = intent.getStringExtra(Urls.MARKER_ID); 
    year = intent.getStringExtra(Urls.MARKER_Year); 
    info = intent.getStringExtra(Urls.MARKER_Info); 

    editTextName = (TextView) findViewById(R.id.markerYear); 
    editTextDesg = (TextView) findViewById(R.id.detailedInfo); 


    editTextName.setText(year); 
    editTextDesg.setText(info); 


    //Saving into picture 

    findViewById(R.id.download).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Bitmap bitmap = takeScreenshot(); // here's where i have to pass the activity 
      saveBitmap(bitmap); 
     } 
    }); 
} 

回答

0

傳遞活動與您傳遞當前活動的上下文相同。您只需要在您的活動中執行以下操作:

takeScreenshot(this) 
+0

'this'將代表沒有活動在這種情況下,因爲是這樣'onClick' – Pragnani

+0

在這種情況下,這將引用您的活動。 takeScreenshot(Activity.this)將活動替換爲您的活動名稱。 –

+0

我不是OP,我已經糾正你的答案..我試圖糾正你,而不是給downvote – Pragnani

0

只需傳遞方法中的活動上下文即可。

Bitmap bitmap = takeScreenshot(MyActivity.this);會爲你工作。

更新

public static Bitmap takeScreenshot(Activity activity) { 
    try { 
     // create bitmap screen capture 
     View v1 = activity.getWindow().getDecorView().getRootView(); 

     v1.setDrawingCacheEnabled(true); 
     Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache()); 
     v1.setDrawingCacheEnabled(false); 

     return bitmap; 

    } catch (Throwable e) { 
     // Several error may come out with file handling or OOM 
     e.printStackTrace(); 
    } 
    return null; 
} 
+0

它不工作,好像當我點擊按鈕截圖時,什麼都沒有發生。函數takeScreenshot有什麼問題嗎? – bhk33

+0

@ bhk33:我已經用我正在使用的內容更新了我的答案。看看這是否有效 – Rohit5k2

+0

@ bhk33你可以看到我的答案在這裏採取screnshot http://stackoverflow.com/questions/35430817/how-to-programmatically-take-a-screenshot-in-android-of-alert-dialog/ 35432168#35432168 – Rohit5k2

0

試試這個捕捉截圖:

public static Bitmap captureScreen(View v) { 

     Bitmap screenshot = null; 
     try { 

      if(v!=null) { 

       screenshot = Bitmap.createBitmap(v.getMeasuredWidth(),v.getMeasuredHeight(), Config.ARGB_8888); 
       Canvas canvas = new Canvas(screenshot); 
       v.draw(canvas); 
      } 

     }catch (Exception e){ 
      Log.d("ScreenShotActivity", "Failed to capture screenshot because:" + e.getMessage()); 
     } 

     return screenshot; 
    } 

    public static void saveImage(Bitmap bitmap) throws IOException{ 

     ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 40, bytes); 
     File f = new File(Environment.getExternalStorageDirectory() + File.separator + "test.png"); 
     f.createNewFile(); 
     FileOutputStream fo = new FileOutputStream(f); 
     fo.write(bytes.toByteArray()); 
     fo.close(); 
    } 

更多參考訪問以下鏈接:

http://karanbalkar.com/2014/03/get-screenshot-of-device-screen-in-android/

+0

這是否捕獲整個屏幕,包括未顯示的文本(必須向上或向下滾動)? – bhk33

相關問題