0

This is XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout_root"
tools:context=".MainActivity">
<ImageView
    android:id="@+id/image_layout"
    android:layout_width="match_parent"
    android:src="@android:drawable/btn_star_big_on"
    android:layout_height="wrap_content"
    android:layout_marginTop="109dp" />
</RelativeLayout>

This is MainActivity.class

public class MainActivity extends AppCompatActivity {
ImageView mImageView;
RelativeLayout mLayoutRoot;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mImageView = (ImageView) findViewById(R.id.image_layout);
    mLayoutRoot = (RelativeLayout) findViewById(R.id.layout_root);
    takeScreenAndLoadToImageView();
}

private void taleScreenAndLoadToImage{
 // What must I do.
}

How to I can take current screen activity and load image to ImageView?

Please. Help me!

Thang BA
  • 162
  • 3
  • 13

3 Answers3

1

Use below code

private void taleScreenAndLoadToImage{
  // What must I do.
  Bitmap bitmap;
  mLayoutRoot.setDrawingCacheEnabled(true);
  bitmap = Bitmap.createBitmap(mLayoutRoot.getDrawingCache());
  mLayoutRoot.setDrawingCacheEnabled(false);
  mImageView.setImageBitmap(bitmap);
}
Passiondroid
  • 1,573
  • 1
  • 16
  • 28
  • I tried. It show bug: Caused by: java.lang.NullPointerException at android.graphics.Bitmap.createBitmap(Bitmap.java:612) – Thang BA Mar 18 '16 at 06:48
  • I tried. But. It not working? Caused by: java.lang.NullPointerException at android.graphics.Bitmap.createBitmap(Bitmap.java:612 – Thang BA Mar 18 '16 at 06:55
  • mLayoutRoot.setDrawingCacheEnabled(true); mLayoutRoot.setDrawingCacheEnabled(false);. Why? True or False? – Thang BA Mar 18 '16 at 06:56
0

First, add proper permission to save file:

And this is the Method for Taking Screen Shot of Current Screen.

private void takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

try {
    // image naming and path  to include sd card  appending name you choose for file
    String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";

    // create bitmap screen capture
    View v1 = getWindow().getDecorView().getRootView();
    v1.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);

    //set the Image at here

     imageview.setImageBitmap (bitmap );
    File imageFile = new File(mPath);

    FileOutputStream outputStream = new FileOutputStream(imageFile);
    int quality = 100;
    bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
    outputStream.flush();
    outputStream.close();

    //openScreenshot(imageFile);
} catch (Throwable e) {
    // Several error may come out with file handling or OOM
    e.printStackTrace();
}
}
Rajan Bhavsar
  • 1,977
  • 11
  • 25
  • Duplicate answer:http://stackoverflow.com/questions/2661536/how-to-programmatically-take-a-screenshot-in-android – Amy Mar 18 '16 at 06:50
  • @Amy This is my working code of And i also checked out that reference link can you please find out the //set the Image at here imageview.setImageBitmap (bitmap ); These two lines please chek my answer first. – Rajan Bhavsar Mar 18 '16 at 06:52
  • I tried. But It not working. It not show image to ImageView :( – Thang BA Mar 18 '16 at 07:00
  • What is the View v1 ?. – Thang BA Mar 18 '16 at 07:03
  • java.lang.NullPointerException 03-18 14:02:58.539 15060-15060/com.example.nguyentrunghieu.animationproject W/System.err: at android.graphics.Bitmap.createBitmap(Bitmap.java:612) – Thang BA Mar 18 '16 at 07:04
0

There are two ways to do it: First one:

public static Bitmap getBitmapFromView(View view) {
        //Define a bitmap with the same size as the view
        Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
        //Bind a canvas to it
        Canvas canvas = new Canvas(returnedBitmap);
        //Get the view's background
        Drawable bgDrawable =view.getBackground();
        if (bgDrawable!=null) 
            //has background drawable, then draw it on the canvas
            bgDrawable.draw(canvas);
        else 
            //does not have background drawable, then draw white background on the canvas
            canvas.drawColor(Color.WHITE);
        // draw the view on the canvas
        view.draw(canvas);
        //return the bitmap
        return returnedBitmap;
    }

Second one:

public static Bitmap getBitmapFromView(View view) {
   view.setDrawingCacheEnabled(true);
   view.buildDrawingCache();
   Bitmap bm = view.getDrawingCache(); 
   return bm;
}

references: Convert view to bitmap on Android How to convert Views to bitmaps?

Community
  • 1
  • 1
arsena
  • 1,935
  • 19
  • 36