The screenshot of the Android activity is saved and displayed to the album View. The animation of the picture disappears

Today, there is a requirement for the project. Take a screenshot of the Activity interface, save it locally, and then send the picture when you send it instantly

Process: screenshot - > save to local - > send broadcast notice to update Gallery

Paste the renderings first

Screen capture process

Files saved after screen capture

Screenshot picture

1. Write a ScreenCaptureUtil tool to capture and save the screen. The code is as follows

/**
 * Created by nxm on 2018/1/20.
 */

public class ScreenCaptureUtil {

    private static ScreenCaptureUtil instance = null;

    private ScreenCaptureUtil() {
    }

    public static ScreenCaptureUtil getInstance() {
        if (null == instance) {
            instance = new ScreenCaptureUtil();
        }
        return instance;
    }

    public Bitmap getScreen(Activity activity) {
        View dView = activity.getWindow().getDecorView();
        dView.setDrawingCacheEnabled(false);
        dView.buildDrawingCache();
        Bitmap bitmap = Bitmap.createBitmap(dView.getDrawingCache());
        if (bitmap != null) {
            try {
                // Save picture first
                String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "HIS";
                File appDir = new File(storePath);
                if (!appDir.exists()) {
                    appDir.mkdir();
                }
                String fileName = System.currentTimeMillis() + ".jpg";
                File file = new File(appDir, fileName);

                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
                bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
                bos.flush();
                bos.close();

                Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                Uri uri = Uri.fromFile(file);
                intent.setData(uri);
                activity.sendBroadcast(intent);
                dView.destroyDrawingCache();
                return bitmap;
            } catch (Exception e) {
                return null;
            }
        } else {
            return null;
        }
    }
}

a key:
1) Get activity screenshot to generate Bitmap

   View dView = activity.getWindow().getDecorView();
   dView.setDrawingCacheEnabled(false);
   dView.buildDrawingCache();
   Bitmap bitmap = Bitmap.createBitmap(dView.getDrawingCache());

2) Create file path save bitmap (save to album root - > file. Separator)

     // Save picture first
                String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "HIS";
                File appDir = new File(storePath);
                if (!appDir.exists()) {
                    appDir.mkdir();
                }
                String fileName = System.currentTimeMillis() + ".jpg";
                File file = new File(appDir, fileName);

                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
                bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
                bos.flush();
                bos.close();

3) Send a broadcast notification to refresh the album to join this picture

 Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
 Uri uri = Uri.fromFile(file);
 intent.setData(uri);
 activity.sendBroadcast(intent);

4) Destroy cache (do not destroy, the first piece of content is always saved)

  dView.destroyDrawingCache();

2. Used in activity (the project requirement is to take a screenshot, display it, and then the animation exits and disappears)

 case R.id.btn_two:
                //Screenshots
                Bitmap screen = ScreenCaptureUtil.getInstance().getScreen(MainActivity.this);
                String content = "";
                if (null != screen) {
                    show_screen.setVisibility(View.VISIBLE);
                    showImage.setImageBitmap(screen);
                    Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scaleanimation);
                    animation.setFillAfter(true);
                    showImage.startAnimation(animation);
                    content = "Screen capture successful";
                } else {
                    content = "Screen capture failed";
                }
                Toast.makeText(MainActivity.this, content, Toast.LENGTH_SHORT).show();
                break;

a key:
1) Load the animation and display (judge whether it is saved successfully first, i.e. whether the bitmap is equal to null)

   Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scaleanimation);
            animation.setFillAfter(true);
            showImage.startAnimation(animation);

2) Animation scaleanimation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="1000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:pivotX="5%"
        android:pivotY="5%"
        android:toXScale="0.0"
        android:toYScale="0.0" />
</set>

Remember to add read and write permissions

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Tags: Android xml encoding

Posted on Sat, 02 May 2020 06:41:57 -0700 by partypete