android - 打开透明 Activity 并截图

标签 android android-intent android-activity android-bitmap

我想截取设备当前屏幕的屏幕截图(无需 root)。所以我开启了一个截屏的透明activity,这样就可以获取到当前设备屏幕的截图了。但是我收到如下所述的错误。

P.S:如果我将 takeScreen() 方法放在 MainActivity 类中,我就可以获得屏幕截图图像。但我不想这样。

MainActivity.java

public class MainActivity extends Activity {
    Bitmap bitmap;

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

    public void btn_1(View view) {
        Intent mainIntent = new Intent(this, transparentActivity.class);
        startActivity(mainIntent);
    }
}

transparentActivity.java

public class transparentActivity extends Activity {

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

    @Override
    protected void onStart() {
        super.onStart();
        Bitmap image = takeScreen();
    }

    public Bitmap takeScreen() {
        View v1 = getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);
        return bitmap;
    }
}

主要布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <Button xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/button1"
        style="?android:attr/buttonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="center_horizontal|center"
        android:layout_marginStart="235dp"
        android:layout_marginBottom="99dp"
        android:onClick="btn_1"
        android:text="Button 1" />
</RelativeLayout>

透明布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent">

</android.support.constraint.ConstraintLayout>

AndroidManifest.xml

    <activity
        android:name=".MainActivity"
        android:exported="true"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

<activity
    android:name=".transparentActivity"
    android:exported="true"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />

我在运行代码时遇到以下错误。

java.lang.RuntimeException: Unable to start activity ComponentInfo{transparentActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference

最佳答案

您的方法不会截取透明 Activity 背后的屏幕截图,因为此方法仅保存特定 View 或布局的内容,而不是您在屏幕上实际看到的内容。

所以请尝试使用媒体投影 API - https://developer.android.com/reference/android/media/projection/package-summary正如评论中所建议的那样。

对于错误部分,请点击 android studio 中的错误消息 - 这会将光标移至发生错误的行,希望您能快速得出结论。

关于android - 打开透明 Activity 并截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53470361/

相关文章:

android - 升级到 Android Studio 3.5 后应用程序未运行

android - Android Studio:找不到方法实现()

android - Google 应用内计费,升级到 Android L Dev Preview 后出现 IllegalArgumentException : Service Intent must be explicit,

java - "this"在例如 Intent(this, ClassName.class) 中使用时如何实际提供来自 Activity 的上下文

java - 如何在不同的 Activity 中调用/调用重复按钮的类

android - 从 ADB shell 启动共享 Activity

android - SharedPreferences 清除/保存

java - JSONObject.toString() 返回 OutOfMemoryError

android - FLAG_ACTIVITY_CLEAR_TOP 和 onActivityResult

android - 从视频 ffmpeg 中提取 alpha