Sunday, 20 July 2014

how to screen capture in android example

ScreenshotActivity.java

package com.androidsurya.screenshot;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class ScreenshotActivity extends Activity {

    Button btn_screenshoot;
    int i = 0;
    ImageView imgv_showscreenshot;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btn_screenshoot = (Button) findViewById(R.id.btn_screenshoot);
        btn_screenshoot.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                View view = findViewById(R.id.relativelayout);
                view.setDrawingCacheEnabled(true);
                Bitmap bitmap = view.getDrawingCache();
                BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
                imgv_showscreenshot = (ImageView) findViewById(R.id.imgv_showscreenshot);                            imgv_showscreenshot.setBackgroundDrawable(bitmapDrawable);

                if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                 
                    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(ScreenshotActivity.this,
                               "File exported to /sdcard/ScreenShots/screenshot"+ i + ".jpg",
                                Toast.LENGTH_SHORT).show();                
                                  i++;                                    
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        }
                    }

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

            }
        });

    }

}

No comments:

Post a Comment