-1

I want to take the screen shot of current screen when,I click a button.Can anyone provide a android code.I need to save the image in the gallery also.this is what i tried

public class CaptureScreenShots extends Activity {
LinearLayout L1;
ImageView image;
 @Override
     protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen_shots);
    L1 = (LinearLayout) findViewById(R.id.LinearLayout01);
    Button but = (Button) findViewById(R.id.munchscreen);
    but.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            View v1 = L1.getRootView();
            v1.setDrawingCacheEnabled(true);
            Bitmap bm = v1.getDrawingCache();
            BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
            image = (ImageView) findViewById(R.id.screenshots);
            image.setBackgroundDrawable(bitmapDrawable);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.screen_shots, menu);
    return true;
}

}
user3409897
  • 143
  • 1
  • 2
  • 9
  • 1
    I'm voting to close this question as off-topic because "can I haz teh code?" questions are off-topic. Please show us what you have done. – An SO User Sep 17 '15 at 06:55
  • 1
    similar ques http://stackoverflow.com/questions/2661536/how-to-programatically-take-a-screenshot-on-android – Abhishek Sep 17 '15 at 06:55
  • public class CaptureScreenShots extends Activity { LinearLayout L1; ImageView image; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.screen_shots); L1 = (LinearLayout) findViewById(R.id.LinearLayout01); Button but = (Button) findViewById(R.id.munchscreen); but.setOnClickListener(new OnClickListener() { – user3409897 Sep 17 '15 at 07:00
  • @Override public void onClick(View v) { View v1 = L1.getRootView(); v1.setDrawingCacheEnabled(true); Bitmap bm = v1.getDrawingCache(); BitmapDrawable bitmapDrawable = new BitmapDrawable(bm); image = (ImageView) findViewById(R.id.screenshots); image.setBackgroundDrawable(bitmapDrawable); } }); } – user3409897 Sep 17 '15 at 07:01
  • i am a beginner,sorry for my mistake @Little Child – user3409897 Sep 18 '15 at 07:22

3 Answers3

5

set button click listener

share = (Button)findViewById(R.id.share);
share.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Bitmap bitmap = takeScreenshot();
        saveBitmap(bitmap);

    }
});

then you have to create two method

public Bitmap takeScreenshot() {
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}

public void saveBitmap(Bitmap bitmap) {
File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
FileOutputStream fos;
try {
    fos = new FileOutputStream(imagePath);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
    fos.flush();
    fos.close();
} catch (FileNotFoundException e) {
    Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
    Log.e("GREC", e.getMessage(), e);
}
}

After add this code into your app, run the app and check your local storage, you have created screen shot of whole page.

Mani kandan
  • 359
  • 4
  • 12
3
 private void getScreen(){
            View v = view.getRootView();
            v.setDrawingCacheEnabled(true);
            Bitmap b = v.getDrawingCache();
            String extr = Environment.getExternalStorageDirectory().toString();
            File myPath = new File(extr, getString(R.string.free_tiket)+".jpg");
            FileOutputStream fos = null;
            try {
            fos = new FileOutputStream(myPath);
            b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
            MediaStore.Images.Media.insertImage( getContentResolver(), b, "Screen", "screen");
            } catch (FileNotFoundException e) {
               e.printStackTrace();
            } catch (Exception e) {
               e.printStackTrace();
        }
}
H.Sarxha
  • 157
  • 1
  • 9
0

I followed your code,when i am click the button image will stored as black screen because i developed with surfaceview custom camera...What will do complete image save in local storage.

ravi
  • 474
  • 1
  • 3
  • 11