sábado, 16 de junio de 2012

Realizar una captura de pantalla

En este post vamos a explicar una forma fácil y rápida de añadir esta funcionalidad a nuestra aplicación. En el código que se muestre se realiza la captura de toda la pantalla. No obstante, si se desea se puede realizar de una vista en concreto ya sea un layout contenido por el layout general de la vista, una imageView, TableView etc...cualquier elemento del xml (layout/xml). El código es el siguiente:

AndroidManifest.xml (Añadir el siguiente permiso)
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_to_capture"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/my_image_capture"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/button_screen_capture"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/my_image_capture"
        android:text="capture Screen" />

</RelativeLayout>

src/ScreenCaptureActivity.java
public class ScreenCaptureActivity extends Activity {

    private String SCREEN_CAPTURE_PATH = Environment
            .getExternalStorageDirectory() + File.separator + "ScreenCaptureFolder";

    private RelativeLayout layoutToCapture;
    private Button buttonScreenCapture;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        buttonScreenCapture = (Button) findViewById(R.id.button_screen_capture);
        layoutToCapture = (RelativeLayout) findViewById(R.id.layout_to_capture);

        buttonScreenCapture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonScreenCapture.setVisibility(View.GONE);
                captureScreen();
                buttonScreenCapture.setVisibility(View.VISIBLE);
            }
        });
    }

    private void captureScreen() {
       
        layoutToCapture.setDrawingCacheEnabled(true);
        layoutToCapture.buildDrawingCache();
        Bitmap croppedBitmap = Bitmap.createBitmap(layoutToCapture
                .getDrawingCache());
        savePhotoToSDCard(croppedBitmap, false);
        layoutToCapture.setDrawingCacheEnabled(false);
    }

    protected boolean savePhotoToSDCard(Bitmap bitmapToSave, boolean needNumFoto) {
        try {
            File directory;
            if (isSDCARDMounted()) {

                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                bitmapToSave.compress(Bitmap.CompressFormat.PNG, 90, bytes);

                directory = new File(SCREEN_CAPTURE_PATH);
                directory.mkdirs();

                directory.createNewFile();

                FileOutputStream fo = null;
                fo = new FileOutputStream(directory + "myScreenCapture.png");

                fo.write(bytes.toByteArray());

                updateGallery();

                Toast.makeText(getApplicationContext(), "Photo saved ok",
                        Toast.LENGTH_LONG).show();

                return true;
            } else {
                Toast.makeText(getApplicationContext(), "no SDCARD found",
                        Toast.LENGTH_LONG).show();
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), "error processing photo",
                    Toast.LENGTH_LONG).show();
            return false;
        }
    }

    private boolean isSDCARDMounted() {
        String status = Environment.getExternalStorageState();
        if (status.equals(Environment.MEDIA_MOUNTED))
            return true;
        return false;
    }

    private void updateGallery() {
        sendBroadcast(new Intent(
                Intent.ACTION_MEDIA_MOUNTED,
                Uri.parse("file://" + Environment.getExternalStorageDirectory())));
    }
}

It works!

Roger Sala,

No hay comentarios:

Publicar un comentario