I am trying to take screenshot of the top running activity view programmatically using the following code and then I'll share the bitmap to a socket server program every interval. After a long research I got this code working. But the issue is, This code is not taking top running activity (whichever the top running activity in my app), instead it is taking screenshot of the particular activity only where this code is written. It is not taking screenshot of any activity which is running on the foreground. Could someone please advise, what may be wrong here?
private Runnable mUpdate = new Runnable() {
public void run() {
//ActivityManager mActivityManager = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
//List<ActivityManager.RunningTaskInfo> task = mActivityManager.getRunningTasks(1);
//ComponentName componentInfo = task.get(0).topActivity;
//mActivityManager.getRunningTasks(1).get(0).topActivity.
//View view = getWindow().getDecorView().findViewById(android.R.id.content);
//getWindow().findViewById(android.R.id.content)
View view = getWindow().getDecorView().findViewById(android.R.id.content);
context = getApplicationContext();
try {
// HERE IS THE SCREENSHOT TAKEN PROGRAMMATICALLY
bitmap = loadBitmapFromView(context,view);
//bitmap = takeScreenshot();
output = ((GlobalClass) RandomIDActivity.this.getApplication()).socket.getOutputStream();
Log.d("ClientActivity", "C: image writing.");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 70, stream);
byte[] imgbyte = stream.toByteArray();
String endStr = "END";
byte[] endByte = endStr.getBytes();
byte[] finalByteToSend = new byte[imgbyte.length + endByte.length];
System.arraycopy(imgbyte, 0, finalByteToSend, 0, imgbyte.length);
System.arraycopy(endByte, 0, finalByteToSend, imgbyte.length, endByte.length);
output.write(finalByteToSend,0,finalByteToSend.length);
output.flush();
imgbyte = null;
endByte = null;
finalByteToSend = null;
}
catch (Exception ex) {
ex.printStackTrace();
}
shareHandler.postDelayed(this, 5000);
}
};
Extracting as bitmap here,
public static Bitmap loadBitmapFromView(Context context, View v) {
DisplayMetrics dm = context.getResources().getDisplayMetrics();
v.measure(MeasureSpec.makeMeasureSpec(dm.widthPixels, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(dm.heightPixels, MeasureSpec.EXACTLY));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Bitmap returnedBitmap = Bitmap.createBitmap(v.getMeasuredWidth(),
v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
v.draw(canvas);
return returnedBitmap;
}
UPDATE CODE:
ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> task = mActivityManager.getRunningTasks(1);
String currActivityString = task.get(0).topActivity.getClassName();
try {
Log.d("currActivityString: ", "currActivityString: " + currActivityString);
Log.d("Test 1111" , "Test 1111");
myCurrClass = Class.forName(currActivityString);
Log.d("Test 2222" , "Test 2222");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Log.d("Test 3333" , "Test 3333");
activityObj = (Activity) myCurrClass.newInstance();
Log.d("Test 4444" , "Test 4444");
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*// CRASHING IN THIS LINE as Fatal Exception: NullPointerException*
View view = activityObj.getWindow().getDecorView().getRootView();
bitmap = loadBitmapFromView(activityObj,view);