0

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 :

Known Issue

1 Answers1

0

This function will return you bitmap of current activity. so you have to store if you want to use later on, or if you want in the same application then you can use with the bitmap object

public static Bitmap captureScreenshot(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bitmap = view.getDrawingCache();
    Rect rect = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
    int statusBarHeight = rect.top;
    @SuppressWarnings("deprecation")
    int width = activity.getWindowManager().getDefaultDisplay().getWidth();
    @SuppressWarnings("deprecation")
    int height = activity.getWindowManager().getDefaultDisplay().getHeight();
    Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, statusBarHeight, width, height - statusBarHeight);
    view.destroyDrawingCache();
    return bitmap2;
}

And here is the function for save bitmap :

private void saveImagetoSDCard(Bitmap bitmap) 
{
    Bitmap bit=takeScreenshot(DisplayImage.this);

    try 
    {
        String file_path = Environment.getExternalStorageDirectory().getAbsolutePath();
        File storagePath = new File(Environment.getExternalStorageDirectory() + "/MyCameraApp/");
        file = new File(storagePath, "Yudiz_krrish.png");

        Log.d("TAG","File path after editing :"+file.getPath().toString());
        FileOutputStream fOut = null;

        fOut = new FileOutputStream(file);
        bit.compress(Bitmap.CompressFormat.PNG, 85, fOut);

        fOut.flush();
        fOut.close();
        btn_changeSettiong.setVisibility(View.VISIBLE);
        btn_saveImage.setVisibility(View.VISIBLE);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
  • :Than you for your help.I can't understand takeScreenshot and gives me an exception Caused by: java.lang.IllegalArgumentException: y + height must be <= bitmap.height() at Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, statusBarHeight, width, height - statusBarHeight); Please help me. – sushant_kaura Dec 06 '14 at 07:15