Android自动化——截屏(二)

   获取当前屏幕,然后截屏,并保存在一定的目录下

1、需要获取SD权限

2、按power key来实现截屏

3、缺点是只能截取该APK的界面


package com.my.takeimage;

import java.io.File;
import java.io.FileOutputStream;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

public class MainActivity extends Activity {

    private final BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(final Context context, final Intent intent) {
            final String action = intent.getAction();
            if (Intent.ACTION_SCREEN_OFF.equals(action)) {
                GetandSaveCurrentImage("test_001","jpg");
                Log.e("TAG", "Screen turn off");
            } else if (Intent.ACTION_SCREEN_ON.equals(action)) {
                Log.e("TAG", "Screen turn on");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
        IntentFilter filter1 = new IntentFilter(Intent.ACTION_SCREEN_ON);
        registerReceiver(mBatInfoReceiver, filter);
        registerReceiver(mBatInfoReceiver, filter1);
        
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    public void onDestory() {
        if (mBatInfoReceiver != null) {
            try {
                unregisterReceiver(mBatInfoReceiver);
            } catch (Exception e) {
                Log.e("TAG", "unregisterReceiver mBatInfoReceiver failure :"
                        + e.getCause());
            }
        }
    }
    private void GetandSaveCurrentImage(String name,String format)   
    {     
        WindowManager windowManager = getWindowManager();   
        Display display = windowManager.getDefaultDisplay();   
        int w = display.getWidth();   
        int h = display.getHeight();   
        
        Bitmap Bmp = Bitmap.createBitmap( w, h, Config.ARGB_8888 );   
        View decorview = this.getWindow().getDecorView();
        decorview.setDrawingCacheEnabled(true);    
        Bmp = decorview.getDrawingCache();    
        
        String SavePath = getSDCardPath()+"/AndyDemo/ScreenImage";
        try {   
            File path = new File(SavePath);   
            String filepath = SavePath + "/" + name + "." + format;   
            File file = new File(filepath);   
            if(!path.exists()){   
                path.mkdirs();   
            }   
            if (!file.exists()) {   
                file.createNewFile();   
            }   
               
            FileOutputStream fos = null;   
            fos = new FileOutputStream(file);   
            if (null != fos) {   
                Bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);   
                fos.flush();   
                fos.close();     
                   
                Toast.makeText(this, "截屏文件已保存至SDCard/AndyDemo/ScreenImage/下", Toast.LENGTH_LONG).show();   
            }   
       
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
    }
    
    private String getSDCardPath(){
        File sdcardDir = null;
        boolean sdcardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
        if(sdcardExist){
            sdcardDir = Environment.getExternalStorageDirectory();
        }
        return sdcardDir.toString();
    }
    
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值