2013-03-07 56 views
5

我想知道當前屏幕截圖(按下按鈕後)的代碼是什麼,並將其保存在圖庫中,因爲我沒有帶SD卡的設備。所以我會保存在默認畫廊。謝謝如何以編程方式截圖並將其保存在圖庫中?

+0

這是不可能的,除非你的設備是根植的。 – 323go 2013-03-07 16:24:03

+0

嘗試這一個..... [http://stackoverflow.com/questions/7762643/android-take-screen-shot-programatically][1] [1]:HTTP:/ /stackoverflow.com/questions/7762643/android-take-screen-shot-programatically – 2014-04-08 16:54:30

回答

0

由於323go評論,這是不可能的,除非你的設備是真正的植根。

但是,如果是這樣,它可能是一個好工作monkeyrunner或如果您使用模擬器。

9
Bitmap bitmap; 
    View v1 = findViewById(R.id.rlid);// get ur root view id 
    v1.setDrawingCacheEnabled(true); 
    bitmap = Bitmap.createBitmap(v1.getDrawingCache()); 
    v1.setDrawingCacheEnabled(false); 

這應該可以做到。

爲了節省

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

你給了上述答案一試? – Raghunandan 2013-03-08 04:02:05

+2

工作正常,答案很好。不要忘記添加權限或它不會工作: 2013-11-21 02:21:43

4
View v1 = L1.getRootView(); 
    v1.setDrawingCacheEnabled(true); 
    Bitmap bm = v1.getDrawingCache(); 
    BitmapDrawable bitmapDrawable = new BitmapDrawable(bm); 
    image = (ImageView) findViewById(R.id.screenshots); 
    image.setBackgroundDrawable(bitmapDrawable); 

有關完整的源代碼,經過下面的博客

http://amitandroid.blogspot.in/2013/02/android-taking-screen-shots-through-code.html

爲了存儲位圖,看看下面的鏈接

Android Saving created bitmap to directory on sd card

1

這將保存到畫廊。該代碼還設置了一個圖像路徑..這對於Intent.SEND_ACTION和電子郵件意圖很有用。

String imagePath = null; 
Bitmap imageBitmap = screenShot(mAnyView); 
if (imageBitmap != null) { 
    imagePath = MediaStore.Images.Media.insertImage(getContentResolver(), imageBitmap, "title", null); 
} 


public Bitmap screenShot(View view) { 
    if (view != null) { 
     Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), 
       view.getHeight(), Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(bitmap); 
     view.draw(canvas); 
     return bitmap; 
    } 
    return null; 
} 
+0

我知道這是幾個月大,但它有點工作...除.. ..它捕獲了視圖的內容而不是子視圖。如何獲得視圖頂部顯示的所有內容(又名整個屏幕)的位圖? – ByteSlinger 2018-02-18 00:14:37

相關問題