Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在没有通知和导航栏的情况下以编程方式拍摄android应用程序的屏幕截图_Android - Fatal编程技术网

如何在没有通知和导航栏的情况下以编程方式拍摄android应用程序的屏幕截图

如何在没有通知和导航栏的情况下以编程方式拍摄android应用程序的屏幕截图,android,Android,我使用下面的代码在我的应用程序中截图 View rootView = findViewById(android.R.id.content).getRootView(); rootView.setDrawingCacheEnabled(true); rootView.buildDrawingCache(true); Bitmap bitmap = rootView.getDrawingCache(); 但是我在通知栏和导航栏上看到了一幅顶部和底部有两个黑色部分的图像。我需要清除那些黑色区域。我不

我使用下面的代码在我的应用程序中截图

View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
rootView.buildDrawingCache(true);
Bitmap bitmap = rootView.getDrawingCache();

但是我在通知栏和导航栏上看到了一幅顶部和底部有两个黑色部分的图像。我需要清除那些黑色区域。我不需要将这些区域替换为通知栏和导航栏。但是我需要工具栏。

使用下面的方法:

public static Bitmap loadBitmapFromView(Context context, View v) {
    DisplayMetrics dm = context.getResources().getDisplayMetrics();
    v.measure(View.MeasureSpec.makeMeasureSpec(dm.widthPixels, View.MeasureSpec.EXACTLY),
            View.MeasureSpec.makeMeasureSpec(dm.heightPixels, View.MeasureSpec.EXACTLY));
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    Bitmap returnedBitmap = Bitmap.createBitmap(v.getMeasuredWidth(),
            v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(returnedBitmap);
    v.draw(c);

    return returnedBitmap;
}

将父视图传递到参数中。

将其传递到另一个线性布局

  <LinearLayout 
   android:id="@+id/linear"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">       

   <Button
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="@string/munch"
     android:id="@+id/munchscreen"/>
   <ImageView
     android:layout_width="200dp"
     android:layout_height="200dp"
     android:id="@+id/screenshots"
     android:contentDescription="@string/app_name"
     />
  </LinearLayout>
现在捕获此屏幕:

 ll.setDrawingCacheEnabled(true);

 ll.buildDrawingCache(true);

 Bitmap cs = Bitmap.createBitmap(ll.getDrawingCache());

 ll.setDrawingCacheEnabled(false);

现在将此位图设置为ImageView。

获取根布局的图形缓存,如下所示:

//onCreate
setContentView(R.layout.activity_main);
layout = (RelativeLayout) findViewById(R.id.activity_main);
layout.setDrawingCacheEnabled(true);
为了得到位图,通常的方法

Bitmap bitmap = layout.getDrawingCache();
主要活动:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
    android:id="@+id/toolBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@color/colorPrimary" />

    <!--other content-->

</RelativeLayout>

以下是XML布局文件,我在线性布局中添加了按钮、文本视图和图像视图。我还在按钮中添加了onClick属性

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_screenshoat"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.ncrypted.myapplication.Screenshoat">

        <Button
            android:id="@+id/btnScreenshot"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Taake screen shot"/>
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/imageView"/>

    </LinearLayout>
添加读写外部存储器的权限

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


是否要捕获屏幕的特定部分?否。我想要除noti之外的所有内容。。还有纳维。。我正在发布一个答案,请尝试一下。可能对你有用。发布你的屏幕截图]@ND1010\u出于安全原因,我无法发布屏幕截图。不管怎么说,屏幕截图的顶部和底部都有一个黑色的部分,每个条的大小都相同。但是我会错过工具栏。请将工具栏放在RelativeLayout中
public class Screenshoat extends AppCompatActivity {

    Button btnScreenshot;
    Bitmap mbitmap;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screenshoat);

        btnScreenshot = (Button) findViewById(R.id.btnScreenshot);
        imageView = (ImageView) findViewById(R.id.imageView);
        btnScreenshot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mbitmap = getBitmapOFRootView(v);
                imageView.setImageBitmap(mbitmap);
                createImage(mbitmap);
                Toast.makeText(Screenshoat.this, "Screen shoat created", Toast.LENGTH_SHORT).show();
            }
        });



    }
    public Bitmap getBitmapOFRootView(View v) {
        View rootview = v.getRootView();
        rootview.setDrawingCacheEnabled(true);
        Bitmap bitmap1 = rootview.getDrawingCache();
        return bitmap1;
    }

    public void createImage(Bitmap bmp) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
        File file = new File(Environment.getExternalStorageDirectory() +
                "/capturedscreenandroid.jpg");
        try {
            file.createNewFile();
            FileOutputStream outputStream = new FileOutputStream(file);
            outputStream.write(bytes.toByteArray());
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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