Android 将View 变为 ImageView 保存本地

这个不太难,绘制本页布局为图片 这样的功能

1.截屏


这个对于我这个CV战士来说,一见面我就觉得不可取了,

①.截屏 如果有三方库的话 最好了,没有 只能调 手机自带的,然而自带就意味着权限

②.去除 状态栏,可裁剪

2.图片转换


BlankJ的 AndroidUtilCode 关于 图片的ImageUtils 这个工具类 提供了 view2bitmap  很显然  我们可以将其转换为bitmap.

在提供一个可以将bitmap作为图片 保存到本地相册的方法

public static void saveImageToGallery(Context context, Bitmap bmp) {
        // 首先保存图片
        File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee");
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 其次把文件插入到系统图库
        try {
            MediaStore.Images.Media.insertImage(context.getContentResolver(),
                    file.getAbsolutePath(), fileName, null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // 最后通知图库更新
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(file.getAbsolutePath())));

    }

这个是需要读写权限的

猜你喜欢

转载自blog.csdn.net/FlyPig_Vip/article/details/88287391