0

This is my code and im trying to capture screenshot of my application.I have background as animation(hearts falling)which looks like a live wallpaper.I want to take screenshot of current page>But its not working.I have used a button to take scrrenshot and imageview to show preview.when button is clicked nothing happens>Iam new to android.Plz help.Thanks in advance!!

              View view = findViewById(R.id.Flayout);

        view.setDrawingCacheEnabled(true);
        Bitmap bitmap = view.getDrawingCache();
        BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
        imgshot = (ImageView) findViewById(R.id.imagescreen);
        // set screenshot bitmapdrawable to imageview
        imgshot.setBackgroundDrawable(bitmapDrawable);
        if (Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState())) 
{
    // we check if external storage is available,                      otherwise  
    // display an error message to the user using Toast    Message
        File sdCard =   Environment.getExternalStorageDirectory();
                File directory = new File(sdCard.getAbsolutePath()
                + "/ScreenShots");
                directory.mkdirs();
                String filename = "screenshot" + i + ".jpg";
                File yourFile = new File(directory, filename);

                while (yourFile.exists())
                {
                i++;
                filename = "screenshot" + i + ".jpg";
                yourFile = new File(directory, filename);
                }

                if (!yourFile.exists())
                {
                if (directory.canWrite())
                {
                try
                                      {
                FileOutputStream out = new FileOutputStream(
                yourFile, true);
                bitmap.compress(Bitmap.CompressFormat.PNG, 90,
                out);
                out.flush();
                out.close();
                Toast.makeText(
                ResultActivity.this,
                "File exported to /sdcard/ScreenShots/screenshot"
                + i + ".jpg",
                Toast.LENGTH_SHORT).show();
                i++;
                }
                catch (IOException e)
                {
                e.printStackTrace();
                }

                }
                }
                } else 
                {
                Toast.makeText(ResultActivity.this,
                "Sorry SD Card not available in your Device!",
                Toast.LENGTH_SHORT).show();
                }

             break;
    }

}

  }
anusha
  • 55
  • 2
  • 10

2 Answers2

0

you have not get the root view. try this code

  File file = new File(Environment.getExternalStorageDirectory()+ File.separator + "myimage.jpg");

// create bitmap 
Bitmap bitmap;
View v1 = getWindow().getDecorView().getRootView();   //if this didnt work then try  sol -3 at the bottom of this answer
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);



ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

      file.createNewFile();
      FileOutputStream fo = new FileOutputStream(file);
      fo.write(bytes.toByteArray()); 
  fo.close();

if this didnt work

View v1 = mCurrentUrlMask.getRootView(); 

then try this

View v1 = getWindow().getDecorView().getRootView();

or you can do

 sol -3 )  View v1 = findViewById(R.id.linear_layout_id);// add the root view id




 Don't forget to add permissions or it wont work: 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
0

Well, check this out:

How to programmatically take a screenshot in Android?

Here looks like what you want to do (as i understand), so test it out.

Community
  • 1
  • 1
Ivan Verges
  • 595
  • 3
  • 10
  • 25