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

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

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

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

相关推荐

  • 建立内网Windows补丁服务器「建议收藏」

    建立内网Windows补丁服务器建立内网Windows补丁服务器实施方案一、完成内网WSUS服务器的架设。二、对客户端计算机进行设置。三、进行外网WSUS服务器的架设。建立内网Windows补丁服务器众所周知,Windows系统有着种种安全漏洞和不足,这些都要依靠微软发布的一个个补丁来弥补,但公司内网无法访问Internet,,不能及时地下载更新。补丁的分发是微软应尽的责任,所以他们开发了WindowsServerUpdateServices(简称WSUS)这一软件,可以实现通过一台服务器下载更新

    2022年4月16日
  • python怎么换行输出的数字对齐_python中如何使输出换行「建议收藏」

    Python的print()函数输出时,通常输出结果是整行显示出来的,这时候我们需要考虑一下,我们输出的结果需不需要换行?不需要换行的方法也是嗯容易的的,这里就不多赘述了,来说说如何做到输出换行:常用的转义符方式:\n#-*-coding:utf-8-*-A=”来看看能不能\n换行。”print(A)输出结果来看看能不能换行。使用三引号进行换行:”””value1;value2;value3…

    2022年4月9日
  • SSL探03

    SSL探03

    2022年1月11日
  • 空指针引用故障(空指针解引用)「建议收藏」

    空指针引用故障(空指针解引用)「建议收藏」C语言中的指针机制使得它灵活高效,但由于指针操作不当产生的动态内存错误也有很多,比如内存泄漏(MemoryLeakage)、内存的重复释放、空指针解引用(NullPointerDereference)。其中空指针引用故障,也叫空指针解引用是一类普遍存在的内存故障,是程序设计语言中一类常见的动态内存错误。指针变量可以指向堆地址、静态变量和空地址单元。当指针指向无效内存地址时对其引用,有可能…

    2022年11月2日
  • SQLyog详细使用教程[通俗易懂]

    SQLyog详细使用教程[通俗易懂]SQLyog详细使用教程

    2022年4月18日
  • .NET-OOP:.文件读写与XML

    .NET-OOP:.文件读写与XML
            文件读写分五个步骤:
    1.        //创建文件流
    FileStreammyFs=newFileStream(path,FileMode.CreateNew);
    2.         //创建写(读)入器
    StreamWritermySw=newStreamWriter(myFs);
    StreamReadermySf=newStreamReader(mtFs);
    3.       

    2022年9月7日

发表回复

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

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