Хочу сделать скриншот текущего экрана устройства (без рута).Итак, я открываю прозрачное действие, чтобы сделать снимок экрана, чтобы я мог получить снимок экрана текущего устройства.Но я получаю сообщение об ошибке, как упомянуто ниже.
PS: я могу получить снимок экрана, если поместить метод 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;
}
}
main_layout
<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>
transparent_layout
<?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