2014-04-30 34 views
0

所以我正在尋找一種方式來採取無根 ,我發現這個問題stackoverflow question以我自己的過程的屏幕截圖

先生羅賓誰回答了這個問題說 「你只能得到截屏截圖你自己的過程「

1-是的我想爲我自己的印刷機拍攝一個屏幕截圖可能有人提供一些不需要root的代碼?

2-我的想法是做一個透明的活動,然後使用應用程序中的屏幕截圖是可能的嗎?

3-另一件事是他們是無論如何拍攝沒有根和沒有在前臺的屏幕截圖?我曾在玩商店看過很多應用程序,可以拍攝屏幕截圖並且不需要root用戶?有任何想法嗎 ?

回答

0

試試這個:

public static Bitmap screenshot(final View view) { 
    view.setDrawingCacheEnabled(true); 
    view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW); 
    view.buildDrawingCache(); 
    if (view.getDrawingCache() == null) { 
     return null; 
    } 

    final Bitmap screenshot = Bitmap.createBitmap(view.getDrawingCache()); 
    view.setDrawingCacheEnabled(false); 
    view.destroyDrawingCache(); 
    return screenshot; 
} 

原始代碼:https://stackoverflow.com/a/5651242/603270

0

考慮,我們要採取截圖單擊按鈕時,代碼將是這樣的:

findViewById(R.id.button1).setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Bitmap bitmap = takeScreenshot(); 
     saveBitmap(bitmap); 
    } 
}); 

首先,我們應該檢索當前視圖層次結構中的最頂層視圖,然後啓用繪圖緩存,然後調用getDrawingCache()

調用getDrawingCache();將返回表示視圖的位圖,如果緩存被禁用,則返回null,這就是爲什麼setDrawingCacheEnabled(true);在調用getDrawingCache()之前應該設置爲true的原因。

public Bitmap takeScreenshot() { 
    View rootView = findViewById(android.R.id.content).getRootView(); 
    rootView.setDrawingCacheEnabled(true); 
    return rootView.getDrawingCache(); 
} 

這節省了位圖圖像到外部存儲方法:

public void saveBitmap(Bitmap bitmap) { 
    File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png"); 
    FileOutputStream fos; 
    try { 
     fos = new FileOutputStream(imagePath); 
     bitmap.compress(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); 
    } 
} 

由於圖像保存在外部存儲方面,WRITE_EXTERNAL_STORAGE權限應加AndroidManifest到文件:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />