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

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

时间: 2024-09-05 20:27:51

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

android将Bitmap对象保存到SD卡中的方法

  android将Bitmap对象保存到SD卡中的方法          这篇文章主要介绍了android将Bitmap对象保存到SD卡中的方法,涉及Android读写SD卡数据的方法,需要的朋友可以参考下 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 Bitmap logoBitmap = BitmapFactory.decodeResourc

基于Android如何实现将数据库保存到SD卡_Android

有时候为了需要,会将数据库保存到外部存储或者SD卡中(对于这种情况可以通过加密数据来避免数据被破解),比如一个应用支持多个数据,每个数据都需要有一个对应的数据库,并且数据库中的信息量特别大时,这显然更应该将数据库保存在外部存储或者SD卡中,因为RAM的大小是有限的:其次在写某些测试程序时将数据库保存在SD卡更方便查看数据库中的内容. Android通过SQLiteOpenHelper创建数据库时默认是将数据库保存在'/data/data/应用程序名/databases'目录下的,只需要在继承SQ

基于Android如何实现将数据库保存到SD卡

有时候为了需要,会将数据库保存到外部存储或者SD卡中(对于这种情况可以通过加密数据来避免数据被破解),比如一个应用支持多个数据,每个数据都需要有一个对应的数据库,并且数据库中的信息量特别大时,这显然更应该将数据库保存在外部存储或者SD卡中,因为RAM的大小是有限的:其次在写某些测试程序时将数据库保存在SD卡更方便查看数据库中的内容. Android通过SQLiteOpenHelper创建数据库时默认是将数据库保存在'/data/data/应用程序名/databases'目录下的,只需要在继承SQ

编码-Android Camera中使用onPreview里的byte[]data怎样保存到SD卡里

问题描述 Android Camera中使用onPreview里的byte[]data怎样保存到SD卡里 最近在做android多媒体开发,已经实践过录像和录音的功能.现在项目要求,android录制视频时,将原始视频数据保存下来,也就是yuv420sp格式的视频,以便以后对这个原始数据进一步的编码和修改. 最近查资料知道可以使用public void onPreviewFrame(byte[] data,Camera camera){}方法获取预览帧,而byte[]data即为所求的yuv42

Android实现从网络获取图片显示并保存到SD卡的方法_Android

本文实例讲述了Android实现从网络获取图片显示并保存到SD卡的方法.分享给大家供大家参考,具体如下: 问题: 如何不断获取图片并显示出来,达到视频的效果? 代码: public class GetPictureFromInternetActivity extends Activity { private ImageView imageView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInst

Android将应用调试log信息保存在SD卡的方法_Android

把自己应用的调试信息写入到SD卡中. package com.sdmc.hotel.util; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import android.co

Android将应用调试log信息保存在SD卡的方法

把自己应用的调试信息写入到SD卡中. package com.sdmc.hotel.util; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import android.co

android读取Assets图片资源保存到SD卡实例

本文为大家详细介绍下android读取Assets图片资源保存到SD卡的具体实现,感兴趣的各位可以参考下哈,希望对大家有所帮助   复制代码 代码如下: public class ReadBitmap { public void readByte(Context c, String name, int indexInt) { byte[] b = null; int[] intArrat = c.getResources().getIntArray(indexInt); try { AssetM

如何在安装软件时将android视频保存在sd卡

问题描述 如何在安装软件时将android视频保存在sd卡 本人android小白 边学边做了app 现在要实现视频播放的功能 开始准备搭个服务器然后播放网络视频 卡了2天没有任何进展 决定把视频放本地算了 现在也实现了 VideoView videoView = (VideoView) findViewById(R.id.videoView); videoView.setVideoURI(Uri.parse(Environment.getExternalStorageDirectory() .