前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >将图片保存到系统相冊的两种方法[通俗易懂]

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

作者头像
全栈程序员站长
发布2022-07-07 20:12:43
6360
发布2022-07-07 20:12:43
举报

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

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

代码语言:javascript
复制
ContentResolver cr = getContentResolver();
					String url = MediaStore.Images.Media.insertImage(cr, bmp,
							String.valueOf(System.currentTimeMillis()), "");

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

代码语言:javascript
复制
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

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

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

代码语言:javascript
复制
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();
					}

最后把整个方法贴出来:

代码语言:javascript
复制
/**
	 * 將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.cn/116309.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年1月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档