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 ...
随机推荐
- zabbix配置自动发现,故障邮件告警
对网段内的主机进行自动发现,自动加入主机组,自定加入template 创建动作时,类型这里选择discovery 然后将发现的主机加入host group和template: 动作针对的是discov ...
- SQLException: com.mchange.v2.c3p0.ComboPooledDataSource [ java.beans.IntrospectionException: java.lang.reflect.InvocationTargetException [numThreadsAwaitingCheckoutDefaultUser] ] has been closed()
问题:Could not get JDBC Connection; nested exception is java.sql.SQLException: com.mchange.v2.c3p0.Com ...
- I2C总线信号时序总结【转】
本文转载自:https://i.cnblogs.com/EditPosts.aspx?opt=1 I2C总线信号时序总结 总线空闲状态 I2C总线总线的SDA和SCL两条信号线同时处于高电平时,规定 ...
- Several Service Control Manager Issues (Event ID's 7000, 7009, 7011)
https://answers.microsoft.com/en-us/windows/forum/windows_7-performance/several-service-control-mana ...
- jquery 浏览器打印
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- Android本地广播
Android中使用的广播一般是系统全局广播,即发出的广播可以被其他任何应用程序接收到,并且我们也可以接收来自于其他任何应用程序的广播.这样就很容易会引起安全性的问题,比如说我们发送的一些携带关键性数 ...
- python 时间元组转可视化时间
>>> import time >>> time.asctime() 'Fri Jan 4 11:17:20 2019' >>> time.asc ...
- python 时间元组转时间戳
#!/usr/bin/python # -*- coding: UTF- -*- import time print(time.mktime((, , , , , , , , ))) 输出 15382 ...
- An error occurred during installation: No such plugin: cloudbees-folder
解决办法:下载cloudbees-folder.hpi放在目录/usr/local/tomcat/webapps/jenkins/WEB-INF/detached-plugins/下,重启tomcat ...
- Mac OS下 selenium 驱动safari日志
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/bin/java "-javaagent:/Applicat ...