Android全屏截图的方法,返回Bitmap并且保存在SD卡上

简介: Android全屏截图的方法,返回Bitmap并且保存在SD卡上 今天做分享,需求是截图分享,做了也是一个运动类的产品,那好,我们就直接开始做,考虑了一下,因为是全屏的分享,所有很自然而然的想到了View的getDrawingCache()方法来获取Bitmap,看到...

Android全屏截图的方法,返回Bitmap并且保存在SD卡上


今天做分享,需求是截图分享,做了也是一个运动类的产品,那好,我们就直接开始做,考虑了一下,因为是全屏的分享,所有很自然而然的想到了View的getDrawingCache()方法来获取Bitmap,看到网上有人说截取不了WebView上的图片,倒是没有去尝试,因为我们的应用不需要,不过有时间还是要去试试,占占坑,这篇博客只是记录一下知识点,没什么技术含量

我们写个小Sample就好了

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="15dp">

    <ImageView
        android:id="@+id/ivPlay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/btnAllWindow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="截取全屏"/>


</LinearLayout>

点击俺就截图,然后返回一个Bitmap,设置在这个imageview的控件上,拿我们写一个截图的方法


    /**
     * 截取全屏
     *
     * @return
     */
    public Bitmap captureScreenWindow() {
        getWindow().getDecorView().setDrawingCacheEnabled(true);
        Bitmap bmp = getWindow().getDecorView().getDrawingCache();
        return bmp;
    }

OK,那我们的点击事件

    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnAllWindow:
                Bitmap bitmap = captureScreenWindow();
                ivPlay.setImageBitmap(bitmap);
                break;
        }
    }

我们实际来看下效果

这里写图片描述

Ok,但是这样并没有保存在内存卡上,我们需要保存一下,做图片上传的功能对吧,所有,我们再来写一个保存图片的方法


    /**
     * 保存到内存卡
     *
     * @param bitName
     * @param mBitmap
     */
    public void saveBitmapForSdCard(String bitName, Bitmap mBitmap) {
        //创建file对象
        File f = new File("/sdcard/" + bitName + ".png");
        try {
            //创建
            f.createNewFile();
        } catch (IOException e) {

        }
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //原封不动的保存在内存卡上
        mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        try {
            fOut.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

把我们的bitmap通过流保存,同时获取本地的时间命名,我们的点击事件就是这样:

    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnAllWindow:
                Bitmap bitmap = captureScreenWindow();
                ivPlay.setImageBitmap(bitmap);
                long time = System.currentTimeMillis();
                saveBitmapForSdCard("img" + time, bitmap);
                break;
        }
    }

最后的结果

这里写图片描述

OK,这只是一个很简单的截图功能,当然,还有很多其他的方法,大家自己可以去研究一下,全部代码

MainActivity

package com.liuguilin.screenshotssample;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    //截取全屏
    private Button btnAllWindow;
    private ImageView ivPlay;

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

        initView();
    }

    /**
     * 初始化
     */
    private void initView() {
        btnAllWindow = (Button) findViewById(R.id.btnAllWindow);
        btnAllWindow.setOnClickListener(this);
        ivPlay = (ImageView) findViewById(R.id.ivPlay);
    }

    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnAllWindow:
                Bitmap bitmap = captureScreenWindow();
                ivPlay.setImageBitmap(bitmap);
                long time = System.currentTimeMillis();
                saveBitmapForSdCard("img" + time, bitmap);
                break;
        }
    }

    /**
     * 截取全屏
     *
     * @return
     */
    public Bitmap captureScreenWindow() {
        getWindow().getDecorView().setDrawingCacheEnabled(true);
        Bitmap bmp = getWindow().getDecorView().getDrawingCache();
        return bmp;
    }

    /**
     * 保存到内存卡
     *
     * @param bitName
     * @param mBitmap
     */
    public void saveBitmapForSdCard(String bitName, Bitmap mBitmap) {
        //创建file对象
        File f = new File("/sdcard/" + bitName + ".png");
        try {
            //创建
            f.createNewFile();
        } catch (IOException e) {

        }
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //原封不动的保存在内存卡上
        mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        try {
            fOut.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

要注意添加一下权限哦

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

貌似代码就这么点,Demo也可以不用上传了,想一起学习Android的可以加群:555974449

什么?小伙伴你需要?那我上传好了:http://download.csdn.net/detail/qq_26787115/9637229

目录
相关文章
|
Android开发 开发者
Android自定义view之利用drawArc方法实现动态效果
本文介绍了如何通过Android自定义View实现动态效果,重点使用`drawArc`方法完成圆弧动画。首先通过`onSizeChanged`进行测量,初始化画笔属性,设置圆弧相关参数。核心思路是不断改变圆弧扫过角度`sweepAngle`,并调用`invalidate()`刷新View以实现动态旋转效果。最后附上完整代码与效果图,帮助开发者快速理解并实践这一动画实现方式。
381 0
|
存储 Shell Android开发
基于Android P,自定义Android开机动画的方法
本文详细介绍了基于Android P系统自定义开机动画的步骤,包括动画文件结构、脚本编写、ZIP打包方法以及如何将自定义动画集成到AOSP源码中。
1531 2
基于Android P,自定义Android开机动画的方法
|
Android开发
基于android-11.0.0_r39,系统应用的手动签名方法和过程
本文介绍了基于Android 11.0.0_r39版本进行系统应用手动签名的方法和解决签名过程中遇到的错误,包括处理`no conscrypt_openjdk_jni-linux-x86_64`和`RegisterNatives failed`的问题。
1207 2
|
安全 数据库 Android开发
在Android开发中实现两个Intent跳转及数据交换的方法
总结上述内容,在Android开发中,Intent不仅是活动跳转的桥梁,也是两个活动之间进行数据交换的媒介。运用Intent传递数据时需注意数据类型、传输大小限制以及安全性问题的处理,以确保应用的健壯性和安全性。
882 11
|
缓存 Java Shell
Android 系统缓存扫描与清理方法分析
Android 系统缓存从原理探索到实现。
1264 15
Android 系统缓存扫描与清理方法分析
|
存储 缓存 编解码
Android经典面试题之图片Bitmap怎么做优化
本文介绍了图片相关的内存优化方法,包括分辨率适配、图片压缩与缓存。文中详细讲解了如何根据不同分辨率放置图片资源,避免图片拉伸变形;并通过示例代码展示了使用`BitmapFactory.Options`进行图片压缩的具体步骤。此外,还介绍了Glide等第三方库如何利用LRU算法实现高效图片缓存。
502 20
Android经典面试题之图片Bitmap怎么做优化
|
Android开发
Android在rootdir根目录创建自定义目录和挂载点的方法
本文介绍了在Android高通平台的根目录下创建自定义目录和挂载点的方法,通过修改Android.mk文件并使用`LOCAL_POST_INSTALL_CMD`变量在编译过程中添加目录,最终在ramdisk.img的系统根路径下成功创建了`/factory/bin`目录。
1294 11
|
ARouter 测试技术 API
Android经典面试题之组件化原理、优缺点、实现方法?
本文介绍了组件化在Android开发中的应用,详细阐述了其原理、优缺点及实现方式,包括模块化、接口编程、依赖注入、路由机制等内容,并提供了具体代码示例。
659 3
|
开发工具 uml git
AOSP源码下载方法,解决repo sync错误:android-13.0.0_r82
本文分享了下载AOSP源码的方法,包括如何使用repo工具和处理常见的repo sync错误,以及配置Python环境以确保顺利同步特定版本的AOSP代码。
4134 0
AOSP源码下载方法,解决repo sync错误:android-13.0.0_r82
|
Android开发
Android项目架构设计问题之onFirstItemVisibleChanged方法的调用如何解决
Android项目架构设计问题之onFirstItemVisibleChanged方法的调用如何解决
296 0

热门文章

最新文章