Android Bitmap 截屏

本章节我们使用 Bitmap 开发一个截屏的小 demo


  1. 创建一个 空的 Android 项目 cn.twle.android.BitmapCapture

  2. 下载 /static/i/meimei.jpg 并保存到 res/drawable 目录下

  3. 修改 activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btn_capture"
                android:text="截屏" />
    
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="center"
            android:src="@drawable/meimei" />
    </RelativeLayout>
    
  4. 修改 MainActivity.java

    package cn.twle.android.bitmapcapture;
    
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.net.Uri;
    import android.os.Environment;
    import android.provider.MediaStore;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class MainActivity extends AppCompatActivity {
        static ByteArrayOutputStream byteOut = null;
        private Bitmap bitmap = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button btn_capture = (Button) findViewById(R.id.btn_capture);
            btn_capture.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    captureScreen();
                }
            });
        }
    
        public void captureScreen() {
    
            Runnable action = new Runnable() {
                @Override
                public void run() {
                    final View contentView = getWindow().getDecorView();
                    try{
                        bitmap = Bitmap.createBitmap(contentView.getWidth(),
                                contentView.getHeight(), Bitmap.Config.ARGB_8888);
                        contentView.draw(new Canvas(bitmap));
                        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteOut);
                        saveCapture(bitmap);
                    }catch (Exception e){e.printStackTrace();}
                    finally {
                        try{
                            if (null != byteOut)
                                byteOut.close();
                            if (null != bitmap && !bitmap.isRecycled()) {
    //                            bitmap.recycle();
                                bitmap = null;
                            }
                        }catch (IOException e){e.printStackTrace();}
    
                    }
                }
            };
            try {
                action.run();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public void saveCapture(Bitmap bmp) {
    
            // 首先保存图片
            File appDir = new File(Environment.getExternalStorageDirectory(), "image");
            if (!appDir.exists()) {
                appDir.mkdir();
            }
            String fileName = System.currentTimeMillis() + ".jpg";
            File file = new File(appDir, fileName);
            try {
                FileOutputStream fos = new FileOutputStream(file);
                bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                fos.flush();
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            // 其次把文件插入到系统图库
            try {
                MediaStore.Images.Media.insertImage(getContentResolver(),
                        file.getAbsolutePath(), fileName, null);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            // 最后通知图库更新
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" +file.getAbsolutePath())));
        }
    }
    
  5. 修改 AndroidManifest.xml 添加读写 SD 卡权限

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

Android 基础教程

关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.