Android View保存为本地图片

ImageView的保存方式和其它View稍微有些不一样

ImageView

    大概思路:获取到ImageView的bitmap数据,将bitmap数据压缩为图片并输出,关闭输出流

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
//  设置保存路径,现在的路径就是"/storage/emulated/0"目录下的image1.png
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"image1.png");
// 获取到ImageView的drawable,并强转为bitmapDrawable
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();//bitmapDrawable转换为bitmap
// 判断是不是获取到了bitmap
if (bitmap != null){
// 输出流需要捕获异常
try {
// 输出流
FileOutputStream fileOutputStream = new FileOutputStream(file);
// CompressFormat: 压缩格式,png、jpg、webp等
// quality: 图片质量,0-100
// OutputStream: 输出流
bitmap.compress(Bitmap.CompressFormat.PNG,100,fileOutputStream);
fileOutputStream.flush(); //清空io
} catch (IOException e) {
e.printStackTrace();
} finally {
if (file.exists()){
Toast.makeText(getContext(),"保存成功",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getContext(),"保存失败",Toast.LENGTH_SHORT).show();
}
}
}
else {
Toast.makeText(getContext(),"保存失败",Toast.LENGTH_SHORT).show();
}

其它View

    其它View无法像ImageView那样通过getDrawable获取drawable并强转为bitmapDrawable,然后再转换为bitmap并保存;必须给View创建一个bitmap,然后将画面绘制在bitmap中

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
//设置保存路径,现在的路径就是"/storage/emulated/0"目录下的image2.png
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"image2.png");
// 获取它的宽高,用于定义创建的bitmap的宽高
int w = pagerView.getWidth();
int h = pagerView.getHeight();
// width: bitmap的宽
// height: bitmap的高
// Config: bitmap的画质,ARGB_8888代表32位ARGB位图,最高画质
Bitmap bitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
// 向bitmap中绘制View中的控件,否则保存为一片空白
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);//设置canvas画布颜色,默认为透明
pagerView.layout(0,0,w,h);//剪裁View,左边开始位置、顶部开始位置、右边结束位置、底部结束位置
pagerView.draw(canvas);//将view画到canvas中

// 然后和正常流程一样了
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG,100,fileOutputStream);
fileOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (file.exists()){
Toast.makeText(getContext(),"保存成功",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getContext(),"保存失败",Toast.LENGTH_SHORT).show();
}
}

    不要忘记赋予储存读写权限

附,Android文件复制

    读取后直接写入,读多少,写多少,简单粗暴

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
//  源文件路径和目标文件路径
File file1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"image2.png");
File file2 = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"image3.png");
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream(file1);
fileOutputStream = new FileOutputStream(file2);
// 字节流
byte[] bytes = new byte[1024];
int len;//剩余长度
while ((len = fileInputStream.read(bytes)) > 0){
fileOutputStream.write(bytes,0,len);//直接写入
}
fileOutputStream.flush();//清空io
} catch (IOException e) {
e.printStackTrace();
} finally {
if (file2.exists()){
Toast.makeText(getContext(),"复制成功",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getContext(),"复制失败",Toast.LENGTH_SHORT).show();
}

if (fileInputStream != null || fileOutputStream != null){
try {
fileInputStream.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}