简体   繁体   English

拍摄全屏的android屏幕截图

[英]Taking android screen shot of whole screen

My application has a list view in relative layout. 我的应用程序具有相对布局的列表视图。 Now I need to take a screen shot of the entire layout including all the content available in the list view and also other contents like button and text view showing in the layout. 现在,我需要对整个布局进行截屏,包括列表视图中可用的所有内容以及布局中显示的其他内容(如按钮和文本视图)。 But my code takes only the visible part of the screen. 但是我的代码仅占用屏幕的可见部分。 Here the view is parent layout. 这里的视图是父布局。

public void screen(){

     View v1 = findViewById(R.id.screenRoot);

    rel.layout(0, 0, rel.getMeasuredWidth(),rel.getMeasuredHeight());    
    View v = v1.getRootView();
    v.setDrawingCacheEnabled(true);

   /* v.measure(MeasureSpec.makeMeasureSpec(w, w), 
            MeasureSpec.makeMeasureSpec(h, h));*/


//        v.layout(0, 0, rel.getWidth(), rel.getHeight());
    v.buildDrawingCache(true);

    System.out.println("rel "+rel.getHeight()+" "+v.getHeight());
    Bitmap b = v.getDrawingCache();  
    v.setDrawingCacheEnabled(false);
    String extr = Environment.getExternalStorageDirectory().toString();
    File myPath = new File(extr, "tiket"+".jpg");
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(myPath);
        b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
        MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
        System.out.println("my path "+myPath+" "+fos);
    }catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Toast.makeText(getApplicationContext(), "Image Captured Successfully", 0).show();

}

Call getBitmapFromView from your Click event or any thing. 从您的Click事件或其他任何事件调用getBitmapFromView。 R.id.root is my main RelativeLayout So you can pass you main layout. R.id.root是我的主要RelativeLayout,因此您可以传递主布局。

LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
RelativeLayout root = (RelativeLayout)inflater.inflate(R.layout.youlayourfile,null);
root.setDrawingCacheEnabled(true);

Bitmap bmp = getBitmapFromView(this.getWindow().getDecorView().findViewById(R.id.root).getRootView());

URI uri = storPrintFile(bmp);

This function returns the Bitmap of your layout. 此函数返回布局的位图。 Now you just need to store the bitmap into your device and anywhere else. 现在,您只需要将位图存储到设备和其他任何位置。

public Bitmap getBitmapFromView(View v) {
    v.setLayoutParams(new LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT));
    v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(),
            v.getMeasuredHeight(), Bitmap.Config.RGB_565);

    Canvas c = new Canvas(b);
    v.draw(c);
    return b;       
}

Storing bitmap into device. 将位图存储到设备中。

public URI storPrintFile(Bitmap bitmapToStore){
    ContextWrapper cw = new ContextWrapper(getApplicationContext());
    String path = cw.getDir("CapturedImages",Context.MODE_PRIVATE).getPath();
    File file = new File(path);
    boolean isFileCreated = false;
    if (!file.exists()) {
        isFileCreated = file.mkdir();
        System.out.println("Directory created in Internal Storage is :" + path);
    }
    String current = "Screen.jpg";//uniqueId.replace(" ", "-").replace(":", "-") + ".jpeg";
    System.out.println("Internal Storage path is : " + current);
    File mypath = new File(file, current);
    try {
        FileOutputStream out = new FileOutputStream(mypath);
        bitmapToStore.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return mypath.toURI();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM