I took this code from internet to capture the screen of the current Activity.
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
Bitmap bitmap = rootView.getDrawingCache();
String str = new SimpleDateFormat("MM_dd_yyyy_HH_mm_ss").format(Calendar.getInstance().getTime()) + ".jpg";
File imagePath = new File(Environment.getExternalStorageDirectory() , str);
Log.i("catpure", "" + Environment.getExternalStorageDirectory() );
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
I have also tried different code. Nothing is working for me. There is no error in logcat. The code runs without any error but still no image in saved. Please let me know where I am wrong. Thanks in advance.
==============================================================================
EDIT
I checked my code and error log. It shows :
Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied).
But I have
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in AndroidManifest.xml.
What can be problem? Please help.
=========================================================================
UPDATE Its a Android 5 issue :