将图片保存到系统相冊的两种方法[通俗易懂]

将图片保存到系统相冊的两种方法

大家好,又见面了,我是全栈君。

第一种:採用系统的api直接使用:

ContentResolver cr = getContentResolver();
					String url = MediaStore.Images.Media.insertImage(cr, bmp,
							String.valueOf(System.currentTimeMillis()), "");

可是,这样的方式必须得刷新图库:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

虽然如此,这样的方法还是仅仅能适合安卓4.4下面的手机,若是4.4以上的手机就会报错。因此建议採用另外一种方式来写。

另外一种:直接採用文件流进行保存到相冊

File tempFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/"
 + String.valueOf(System.currentTimeMillis()) + ".png");
					if(tempFile.exists()){
						tempFile.delete();
					}
					try {
						tempFile.createNewFile();
					} catch (IOException e) {
						e.printStackTrace();
					}
					FileOutputStream fOut = null;
					try {
						fOut = new FileOutputStream(tempFile);
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					}
					bmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);
					try {
						fOut.flush();
						fOut.close();
					} catch (IOException e) {
						// TODO: handle exception
						e.printStackTrace();
					}

最后把整个方法贴出来:

/**
	 * 將ImageView中的圖片保存到系统相冊
	 */
	private void SaveImageToSysAlbum() {
		if (FileUtil.isSdCardExist()) {
			BitmapDrawable bmpDrawable = (BitmapDrawable)mFullImageView.getDrawable();
			Bitmap bmp = bmpDrawable.getBitmap();
			if (bmp != null) {
				try {
					/*ContentResolver cr = getContentResolver();
					String url = MediaStore.Images.Media.insertImage(cr, bmp,
							String.valueOf(System.currentTimeMillis()), "");*/
					File tempFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/"
 + String.valueOf(System.currentTimeMillis()) + ".png");
					if(tempFile.exists()){
						tempFile.delete();
					}
					try {
						tempFile.createNewFile();
					} catch (IOException e) {
						e.printStackTrace();
					}
					FileOutputStream fOut = null;
					try {
						fOut = new FileOutputStream(tempFile);
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					}
					bmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);
					try {
						fOut.flush();
						fOut.close();
					} catch (IOException e) {
						// TODO: handle exception
						e.printStackTrace();
					}
					
					Toast.makeText(this, getString(R.string.save_succ), Toast.LENGTH_SHORT).show();

				} catch (Exception e) {
					e.printStackTrace();
				}
			}else {
				Toast.makeText(this, getString(R.string.no_iamge_save_fail), Toast.LENGTH_SHORT).show();
			}
		}else {
			Toast.makeText(this, getString(R.string.no_sdcard_save_fail), Toast.LENGTH_SHORT).show();
		}
		String release = android.os.Build.VERSION.RELEASE;
		String tempID = release.substring(0, 3);
		if(Double.parseDouble(tempID) >= 4.4){//安卓4.4以上版本号的时候使用这个。下面的使用else语句里面的
			MediaScannerConnection.scanFile(this,new String[]{Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/" }, null,null);
		}else {
			sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
			MediaScannerConnection.scanFile(this,new String[]{Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/" }, null,null); 
		}
		
		
	}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116309.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ubuntu添加静态路由表_Ubuntu配置静态ip

    ubuntu添加静态路由表_Ubuntu配置静态ip使用route命令(添加临时路由)添加到主机的路由#routeadd-host192.168.1.123deveth0#routeadd-host192.168.1.123gw192.168.1.1添加到网络的路由#routeadd-net192.168.1.123netmask255.255.255.0eth0#routeadd-net192.168…

    2022年9月15日
    2
  • performSelector 注意问题及原理

    performSelector 注意问题及原理1 首先使用 performSelec 是要特别注意内存泄漏问题 下面代码演示 创建一个控制器 ZWWTestThrea 从上个控制器 push 到该控制器 ZWWLog 的宏定义 ifdefDEBUG defineZWWLog fmt NSLog s Line d fmt PRETTY FUNCTION

    2025年6月22日
    4
  • 网络天才网页版在线玩无需下载(中国破解网络的天才)

    网络天才这是一款解谜问答类的娱乐游戏,用独具色彩的画面风格,以阿拉丁为故事背景,可以用几个问题来猜出你心中所想,并体验多种思维导向与解谜挑战,非常适合闲暇时间里休闲娱乐一下。感兴趣的快来下载吧!游戏特色释放你的创造力由于Geniz,你可以解锁,并玩转新的背景,随意定制Akinator。这个精灵将变成一个吸血鬼、牛仔或迪斯科舞者。继续猜每天尝试找出5个神秘人物,赢得特别的天才,用来定制你最喜欢的妖怪…

    2022年4月18日
    5.6K
  • python获取当前系统的日期_python怎么获取当前系统时间

    python获取当前系统的日期_python怎么获取当前系统时间python获取当前系统时间,包括年月日,时分秒,主要通过Python中的datetime模块来实现。下面我们就通过具体的代码示例,给大家详细介绍Python获取当前时间日期的实现方法。代码示例如下:importdatetimenow=datetime.datetime.now()print(“当前系统日期和时间是:”)print(now.strftime(“%Y-%m-%d%H:%…

    2022年10月19日
    4
  • 【机器学习】代价函数,损失函数,目标函数区别

    【机器学习】代价函数,损失函数,目标函数区别一:损失函数,代价函数,目标函数定义首先给出结论:损失函数(LossFunction)是定义在单个样本上的,算的是一个样本的误差。代价函数(CostFunction)是定义在整个训练集上的,是所有样本误差的平均,也就是损失函数的平均。目标函数(ObjectFunction)定义为:最终需要优化的函数。等于经验风险+结构风险(也就是CostFunction+正则化项)。关于目标函数和…

    2022年4月29日
    124
  • N皇后问题_Java递归解决N皇后问题

    N皇后问题_Java递归解决N皇后问题18124 N皇后问题时间限制:2000MS 内存限制:65535K提交次数:0通过次数:0题型:编程题   语言:G++;GCC;VCDescription有N*N的国际象棋棋盘,要求在上面放N个皇后,要求任意两个皇后不会互杀,有多少种不同的放法?输入格式每一个数为T,代表CASE的数量,T<=13此后,每行一个数N(13>=N>0)

    2022年9月29日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号