在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保存为图片并放在相册中的更多相关文章

  1. BitmapUtil【缩放bitmap以及将bitmap保存成图片到SD卡中】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 用于缩放bitmap以及将bitmap保存成图片到SD卡中 效果图 代码分析 bitmapZoomByHeight(Bitmap s ...

  2. view保存为图片

    一.概述 简书.微博.便签等都有将文章保存为图片的功能.笔者臆测,此功能的实现原理如下. 二.实现 2.1将View保存成Bitmap对象 方法1(亲测有效) private Bitmap makin ...

  3. Android 自定义View消除锯齿实现图片旋转,添加边框及文字说明

    先看看图片的效果,左边是原图,右边是旋转之后的图:   之所以把这个写出来是因为在一个项目中需要用到这样的效果,我试过用FrameLayout布局如上的画面,然后旋转FrameLayout,随之而来也 ...

  4. android 开发 View _13 绘制图片与BitmapShader位图的图像渲染器

    BitmapShader位图的图像渲染器 TileMode 模式 Shader.TileMode.CLAMP 边缘拉伸. Shader.TileMode.MIRROR 在水平方向和垂直方向交替景象, ...

  5. android 开发 View _8_ 动态图片自定义View

    转载地址:https://blog.csdn.net/mengks1987/article/details/77770922 先来看下效果: 是不是有一种熟悉感,其实这种效果使用序列帧动画也是可以实现 ...

  6. h5页面使用js实现保存当前图片到手机相册

    很可惜,这个鬼东西微信内置浏览器不适用 页面: <!doctype html> <html> <head> <meta charset="UTF-8 ...

  7. iOS开发中,获取图片之后保存或下载到本地相册中

    #pragma mark 先获取本地图片或者网络图片 - (void)saveHeaderImageWith:(NSString *)path { UIImage *img = [UIImage im ...

  8. Android 自定义View及其在布局文件中的使用示例(三):结合Android 4.4.2_r1源码分析onMeasure过程

    转载请注明出处 http://www.cnblogs.com/crashmaker/p/3549365.html From crash_coder linguowu linguowu0622@gami ...

  9. android 开发 View _1_ View的子类们 和 视图坐标系图

    目录: android 开发 View _2_ View的属性动画ObjectAnimator ,动画效果一览 android 开发 View _3_ View的属性动画ValueAnimator a ...

随机推荐

  1. 计算概论(A)/基础编程练习1(8题)/7:奇数求和

    #include<stdio.h> int main() { // 输入非负整数 int m, n; scanf("%d %d", &m, &n); / ...

  2. MySQL备份与恢复-innobackupex

    :上一片myloder搞崩溃,为什么百度的博文都是抄袭一模一样的,哎烦! 这一片文章我们来介绍物理备份工具xtracebackup! 首先是安装可以percona官网下载安装,下载rpm包直接yum安 ...

  3. golang debug调试

    1. debug by gdb: office doc download the runtime-gdb file. $ wget -q -O - https://golang.org/src/run ...

  4. IOS项目中的细节处理,如更改状态栏等等

    一,状态栏更改为白色 1 在info.plist中添加一个字段:view controller -base status bar 为NO 2 在需要改变状态栏颜色的ViewController中在Vi ...

  5. React 回忆录(一)为什么使用 React?

    Hi 各位,欢迎来到 React 回忆录!

  6. Git项目创建与提交

    创建Git密钥: 1.生成密钥: 右键–>Git Bash Here:先输入ssh-keygen –t rsa –C "邮箱地址",注意ssh-keygen之间是没有空格的, ...

  7. JavaScript权威指南2.词法结构

    字符集 1.用16位的Unicode字符集编写的,可以表示地球上通用的每一种书面语言.国际化 2.每个字符都是用两个字节表示的 3.大小写敏感:关键字.变量.函数名.标识符:HTML并不区分大小写 H ...

  8. rg.apache.ibatis.binding.BindingException: Mapper method 'com.dao.Cameao.getOnlineDayRation attempted to return null from a method with a primitive return type (float)

    本文为博主原创,未经允许不得转载: 异常展示如下: org.apache.ibatis.binding.BindingException: Mapper method 'com.dao.Cameao. ...

  9. 《算法竞赛入门经典》习题及反思 -<2>

    数组 Master-Mind Hints,Uva 340 题目:给定答案序列和用户猜的序列,统计有多少数字对应正确(A),有多少数字在两个序列都出现过但位置不对. 输入包括多组数据.每组输入第一行为序 ...

  10. NS3 fifth.cc 拥塞窗口实例

    fifth.cc /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* * This progr ...