概述

今天做的APP中的一个表单页面需要以后蓝牙连接打印设备后打印出来,当时想过要截屏,但想了一下截屏的话太麻烦,截屏的话是将手机的屏幕截取下来,包含很多无用的信息,所以这个想法就给pass掉了;后来又想到了转化为图片、PDF什么的,在网上搜索了一些相关信息后,发现还是将整个布局变成图片比较简单,并且也找到了一篇不错测教程,文末附有链接。

实现流程以及实现代码

设置权限

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

获取布局

  relativeLayout = (RelativeLayout) findViewById(R.id.layout_rl);

设置布局相关设置

 // 获取图片某布局
relativeLayout.setDrawingCacheEnabled(true);
relativeLayout.buildDrawingCache();

获取图片

 final Bitmap bmp = relativeLayout.getDrawingCache(); // 获取图片
 savePicture(bmp, "test.jpg");// 保存图片

保存图片

public void savePicture(Bitmap bm, String fileName) {
    Log.i("xing", "savePicture: ------------------------");
    if (null == bm) {
        Log.i("xing", "savePicture: ------------------图片为空------");
        return;
    }
    File foder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test");
    if (!foder.exists()) {
        foder.mkdirs();
    }
    File myCaptureFile = new File(foder, fileName);
    try {
        if (!myCaptureFile.exists()) {
            myCaptureFile.createNewFile();
        }
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        //压缩保存到本地
        bm.compress(Bitmap.CompressFormat.JPEG, 90, bos);
        bos.flush();
        bos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Toast.makeText(this, "保存成功!", Toast.LENGTH_SHORT).show();

}

释放资源

relativeLayout.destroyDrawingCache();

完整代码如下

package com.adwan.savephototolocal;

import android.graphics.Bitmap;
import android.os.Environment;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {
private RelativeLayout relativeLayout;
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    relativeLayout = (RelativeLayout) findViewById(R.id.layout_rl);

}

public void save(View view) {
    initView();
}
private void initView() {
    // 获取图片某布局
    relativeLayout.setDrawingCacheEnabled(true);
    relativeLayout.buildDrawingCache();

    mHandler.postDelayed(new Runnable() {

        @Override
        public void run() {
            // 要在运行在子线程中
            final Bitmap bmp = relativeLayout.getDrawingCache(); // 获取图片
            savePicture(bmp, "test.jpg");// 保存图片
            relativeLayout.destroyDrawingCache(); // 保存过后释放资源
        }
    },100);
}

public void savePicture(Bitmap bm, String fileName) {
    Log.i("xing", "savePicture: ------------------------");
    if (null == bm) {
        Log.i("xing", "savePicture: ------------------图片为空------");
        return;
    }
    File foder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test");
    if (!foder.exists()) {
        foder.mkdirs();
    }
    File myCaptureFile = new File(foder, fileName);
    try {
        if (!myCaptureFile.exists()) {
            myCaptureFile.createNewFile();
        }
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        //压缩保存到本地
        bm.compress(Bitmap.CompressFormat.JPEG, 90, bos);
        bos.flush();
        bos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Toast.makeText(this, "保存成功!", Toast.LENGTH_SHORT).show();

}

}

参考:

http://www.jianshu.com/p/72242e612ecb#