Thursday 14 February 2013

Android taking Screen shots through code.

In this blog i ll show you how to take screen shots Pragmatically (Through Code) without using DDMS view.



Screen 1) When you click this Munch Screen Button the whole view of this Activity will save into BittmapDrawab

 Screen 2) Final Screen shots

/** layout */
screen_shots.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/LinearLayout01"
    >
<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/munch"
    android:id="@+id/munchscreen"
    />
  <ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/screenshots"
    android:contentDescription="@string/app_name"
    />
 
</LinearLayout>

/** Activity Class */
CaptureScreenShots.java

package com.screen.shots;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

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;
    }

}

/** values/strings.xml */

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">ScreenShots</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="munch">Munch Screen</string>

</resources>


You can download Source code here

14 comments:

  1. Hi... Amit Gupta!
    on which software ( windows) you running this????????? The screen shots you published herabove........

    ReplyDelete
  2. I'm making a simple app, that takes a photo and saves it in a file, for that I've used this http://stackoverflow.com/questions/649154/save-bitmap-to-location
    Thank you so much! Excellent and simple code.

    ReplyDelete
  3. I have tried so many videos all of them while running giving errors in your code its working fine thanks lot for your code can you give me code for continue to send to mail this screen shot.

    ReplyDelete
  4. It's nice that you write about it
    Laura

    ReplyDelete
  5. Offshore Development Center is helping companies develop quality software at lower costs. Learn how ODC work, and which countries are best suited to host them.

    ReplyDelete
  6. I enjoyed reading your blog, and it is both instructional and enjoyable.
    Mobile App Development Company

    ReplyDelete
  7. Website Development Company USA: I would suggest you to have website because it can generate more business than android apps.

    ReplyDelete
  8. Very informative post. Keep posting
    Dxminds is Mobile app development Company in Chennai for your business. Very unique and creative mobile app developers. Find more here: https://dxminds.com/top-mobile-app-development-company-in-chennai/#

    ReplyDelete
  9. Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? cursos de ti

    ReplyDelete
  10. This blog is very useful for all mobile app developers and your explanation was simply straight to the point and easy to understand. I will definitely try to implement your idea. Thank you for this information!

    Mobile App Development Companies in Chennai

    Mobile App Development Company in Chennai

    Android Application Development Company in Dubai

    iOS application development company in Dubai

    ReplyDelete