2013-03-07 58 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

相关问题