Android将view保存为图片并放在相册中
在Android中,可以将view保存为图片并放在相册中,步骤为 view->bitmap->file,即先将view转化为bitmap,再将bitmap保存到相册中。
  
需要将红框标注的view转化为图片并保持到相册中。view的XML代码为
<RelativeLayout
android:id="@+id/lanjing_code_rl" //要保存的view id
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="20dp"
android:paddingBottom="10dp"
android:layout_weight="1"
android:background="@drawable/round_main_bg" > <RelativeLayout
android:id="@+id/lanjing_code_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp" > <ImageView
android:id="@+id/self_head"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:background="@drawable/defaultuser" /> <TextView
android:id="@+id/self_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="@+id/self_head"
android:gravity="center"
android:maxLines="2"
android:textColor="@color/article_black"
android:textSize="16sp"
android:textStyle="bold" /> <TextView
android:id="@+id/self_hospital"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/self_name"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="20dp"
android:layout_marginTop="3dp"
android:layout_toRightOf="@+id/self_head"
android:textColor="@color/article_gray"
android:textSize="14sp" />
</RelativeLayout> <ImageView
android:id="@+id/lanjing_code_img"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_below="@+id/lanjing_code_info"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:scaleType="fitCenter" /> <TextView
android:id="@+id/lanjing_code_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/lanjing_code_img"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:gravity="center"
android:textColor="@color/number_blue"
android:textScaleX="1.5"
android:textSize="23sp" /> <TextView
android:id="@+id/lanjing_tishi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/lanjing_code_txt"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="扫一扫上面的二维码图案,加我蓝鲸医生"
android:textColor="@color/solid_gray"
android:textSize="14sp" />
</RelativeLayout>
保存方法
private void saveErweima() {   
     File erweimaImg = FileUtilss.getErweimaImgFile(MyLanJingCode.this);
        if (erweimaImg == null) {
            return;
        }
        //lanjing_code_rl.setBackgroundColor(getResources().getColor(R.color.solid_white));
        if (false == lanjing_code_rl.isDrawingCacheEnabled()) {
            lanjing_code_rl.setDrawingCacheEnabled(true);
        }
        lanjing_code_rl.buildDrawingCache();
        Bitmap bitmap = lanjing_code_rl.getDrawingCache();
     String path = erweimaImg.getAbsolutePath();
        FileOutputStream out = new FileOutputStream(path);
        bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.flush();
        out.close();
     lanjing_code_rl.setDrawingCacheEnabled(false);
        MediaScannerConnection.scanFile(MyLanJingCode.this, new String[] { Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/" + path }, null, null);
        saveImgToGallery(path);
    }
public boolean saveImgToGallery(String fileName) {
        boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
        if (!sdCardExist)
            return false;
        try {
            ContentValues values = new ContentValues();
            values.put("datetaken", new Date().toString());
            values.put("mime_type", "image/jpg");
            values.put("_data", fileName);
            Application app = DoctorApplication.getInstance();
            ContentResolver cr = app.getContentResolver();
            cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
这样,就能在相册中看到保持的图片了。

Android将view保存为图片并放在相册中的更多相关文章
- BitmapUtil【缩放bitmap以及将bitmap保存成图片到SD卡中】
		
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 用于缩放bitmap以及将bitmap保存成图片到SD卡中 效果图 代码分析 bitmapZoomByHeight(Bitmap s ...
 - view保存为图片
		
一.概述 简书.微博.便签等都有将文章保存为图片的功能.笔者臆测,此功能的实现原理如下. 二.实现 2.1将View保存成Bitmap对象 方法1(亲测有效) private Bitmap makin ...
 - Android 自定义View消除锯齿实现图片旋转,添加边框及文字说明
		
先看看图片的效果,左边是原图,右边是旋转之后的图: 之所以把这个写出来是因为在一个项目中需要用到这样的效果,我试过用FrameLayout布局如上的画面,然后旋转FrameLayout,随之而来也 ...
 - android 开发 View _13  绘制图片与BitmapShader位图的图像渲染器
		
BitmapShader位图的图像渲染器 TileMode 模式 Shader.TileMode.CLAMP 边缘拉伸. Shader.TileMode.MIRROR 在水平方向和垂直方向交替景象, ...
 - android 开发 View _8_ 动态图片自定义View
		
转载地址:https://blog.csdn.net/mengks1987/article/details/77770922 先来看下效果: 是不是有一种熟悉感,其实这种效果使用序列帧动画也是可以实现 ...
 - h5页面使用js实现保存当前图片到手机相册
		
很可惜,这个鬼东西微信内置浏览器不适用 页面: <!doctype html> <html> <head> <meta charset="UTF-8 ...
 - iOS开发中,获取图片之后保存或下载到本地相册中
		
#pragma mark 先获取本地图片或者网络图片 - (void)saveHeaderImageWith:(NSString *)path { UIImage *img = [UIImage im ...
 - Android 自定义View及其在布局文件中的使用示例(三):结合Android 4.4.2_r1源码分析onMeasure过程
		
转载请注明出处 http://www.cnblogs.com/crashmaker/p/3549365.html From crash_coder linguowu linguowu0622@gami ...
 - android 开发 View  _1_   View的子类们 和 视图坐标系图
		
目录: android 开发 View _2_ View的属性动画ObjectAnimator ,动画效果一览 android 开发 View _3_ View的属性动画ValueAnimator a ...
 
随机推荐
- 【翻唱】白金disco
			
http://video.yingtu.co/0/bbea91f8-e1ce-4211-9519-2371745f4ec0.mp4 [翻唱]白金disco
 - P2455 [SDOI2006]线性方程组(real gauss)
			
P2455 [SDOI2006]线性方程组 (upd 2018.11.08: 这才是真正的高斯消元模板) 找到所消未知数(设为x)系数最大的式子,把它提上来 把这个式子的 x 系数约成1 把这个式子用 ...
 - BZOJ1632: [Usaco2007 Feb]Lilypad Pond  SPFA+最短路计数
			
Description 为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘.这个长方形的池子被分成了M行N列个方格(1≤M,N≤30).一些格子是坚固得令人惊讶的莲花,还有一些格子是岩石,其余的只是 ...
 - echart提示框内容数据添加单位
			
本文为博主原创,转载须注明转载地址: 方法为: tooltip : { trigger: 'axis', formatter: '{a0}:{c0}%' }, legend: { data:['测试' ...
 - linux——压缩解压命令学习简单笔记
			
一: 命令名称:gzip 命令英文原意:GNU zip 命令所在路径:/bin/gzip 执行权限:所有用户 语法:gzip 选项 [文件] 功能描述:压缩文件 压缩后文件格式:.gz 1:只能压缩文 ...
 - python 分数的数学四则运算
			
import fractions f1 = fractions.Fraction(, ) f2 = fractions.Fraction(, ) print('{} + {} = {}'.format ...
 - ssh 连接不同无线网且IP以及用户名都相同
			
问题现场及解析 用OpenSSH的人都知ssh会把你每个你访问过计算机的公钥(public key)都记录在~/.ssh/known_hosts. 当下次访问相同计算机时,OpenSSH会核对公钥. ...
 - java编写编译器和解释器
			
on 2012-07-14 21:24 Bang 阅读(102) 评论(0) 编辑 收藏 续 第二部分 初始后端实现 框架后端支持编译器和解释器.现在框架抽象类Backend有两个极简版实现,一个 ...
 - Django模板语言详解
			
本节将介绍Django模版系统的语法.Django模版语言致力于在性能和简单性上取得平衡. 如果你有过其它编程背景,或者使用过一些在HTML中直接混入程序代码的语言,那么你需要记住,Django的模版 ...
 - cygwin install git
			
Installation with Cygwin If you're comfortable with Cygwin, then use it to install git, ssh, wget an ...