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

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

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

第一种:採用系统的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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • NOIP 2012 年普及组初赛试题整理「建议收藏」

    NOIP 2012 年普及组初赛试题整理「建议收藏」问题求解题目答案:5题目解析:贪心法。构造出最坏情况下至多选4点、不在同一条直线上,此时再添加任意一个点,则一定出现3点一线。题目答案:2880题目解析:圆排列问题。5名大陆选手的排列方案数为:5!5名港澳选手安排在大陆选手之间的5个空位的排列方案数为:5!总的排列方案数为:5!×5!。但每位选手左边相邻的选手均相同,为重复方案。最终结果为:阅读程序#include<iostream>usingnamespace..

    2022年8月22日
    4
  • iOS 瀑布流实现「建议收藏」

    iOS 瀑布流实现「建议收藏」一、先来看看最终的效果吧二、创建UI   1.首先我们在viewcontroller中创建一个UICollectionView.//主控制器中#import"ViewController.h"#import"WaterFallCollectionViewCell.h"#import"WaterfallFlowLayout.h"staticconstNSIn…

    2022年10月26日
    0
  • python-tkinter(7) 实现各种个样的撩妹鼠标拖尾

    python-tkinter(7) 实现各种个样的撩妹鼠标拖尾

    2022年2月21日
    45
  • clion永久激活码2021【在线注册码/序列号/破解码】「建议收藏」

    clion永久激活码2021【在线注册码/序列号/破解码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月18日
    287
  • csgo免费开箱网站_csgo开箱网站skincat

    csgo免费开箱网站_csgo开箱网站skincatskinsdog狗网CSGO开箱地址!csgo开箱网站官网!官方链接:skinsdog.cc注册登录自动免费获得$0.8美金推广码:csgogo(注册使用送0.8美金)支付:微信支付宝状态:直接取回

    2022年10月5日
    0
  • metasploit指令_msfconsole下载

    metasploit指令_msfconsole下载在MSF里面msfconsole可以说是最流行的一个接口程序。很多人一开始碰到msfconsole的时候就害怕了。那么多复杂的命令语句需要学习,但是msfconsole真的是一个强大的接口程序。Msfconsole提供了一个一体化的集中控制台。通过msfconsole,你可以访问和使用所有的metasploit的插件,payload,利用模块,post模块等等。Msfconsole还有第三方程序的…

    2022年9月7日
    0

发表回复

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

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