I have create the Handler inside the activity onCreate method. This Hanlder is responsible to take screen shots after 10 seconds. Inside the run method I have used while(flag==true) and screen the capture util flag==false, But this stuck my activity. I can not able to work. And it take the screen shot over again and again of same image because of actvity is stuck. How I can work with my screen and what I am doing handler take the screen shot after 10 seconds? The while loop stuck my app.
It Take picture but I am not able to work with my activity.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flag = true;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
while (flag == true) {
String SCREENSHOTS_LOCATIONS = Environment
.getExternalStorageDirectory().toString() + "/re/";
// Get root view
View view = getWindow().getDecorView().getRootView();
// Create the bitmap to use to draw the screenshot
final Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
// Get current theme to know which background to use
final Theme theme = getTheme();
final TypedArray ta = theme
.obtainStyledAttributes(new int[] { android.R.attr.windowBackground });
final int res = ta.getResourceId(0, 0);
final Drawable background = getResources().getDrawable(res);
// Draw background
background.draw(canvas);
// Draw views
view.draw(canvas);
FileOutputStream fos = null;
try {
final File sddir = new File(SCREENSHOTS_LOCATIONS);
if (!sddir.exists()) {
sddir.mkdirs();
}
fos = new FileOutputStream(SCREENSHOTS_LOCATIONS + x
+ ".jpg");
x++;
if (fos != null) {
if (!bitmap.compress(Bitmap.CompressFormat.JPEG,
90, fos)) {
Log.d("ScreenShot", "Compress/Write failed");
}
fos.flush();
fos.close();
}
} catch (Exception e) {
}
}
}
}, 1000);
}