1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > android 保存图片到手机相册 并通知相册刷新

android 保存图片到手机相册 并通知相册刷新

时间:2024-04-01 13:55:56

相关推荐

android 保存图片到手机相册 并通知相册刷新

保存图片到相册

项目中我们经常用到,将图片保存至手机相册,并通知相册及时刷新,展示图片。

只需将代码复制至图片工具类,直接使用即可;

/*** 保存图片到图库* @param context* @param bmp*/public static void saveImageToGallery(Context context, Bitmap bmp) {// 首先保存图片File appDir = new File(Environment.getExternalStorageDirectory(),"desheng");if (!appDir.exists()) {appDir.mkdir();}String fileName = System.currentTimeMillis() + ".jpg";File file = new File(appDir, fileName);try {FileOutputStream fos = new FileOutputStream(file);press(pressFormat.JPEG, 100, fos);fos.flush();fos.close();} catch (FileNotFoundException e) {MyToastUtils.showShortToast(context, "保存失败");e.printStackTrace();} catch (IOException e) {MyToastUtils.showShortToast(context, "保存失败");e.printStackTrace();}// 其次把文件插入到系统图库try {MediaStore.Images.Media.insertImage(context.getContentResolver(),file.getAbsolutePath(), fileName, null);MyToastUtils.showShortToast(context, "保存成功");} catch (FileNotFoundException e) {MyToastUtils.showShortToast(context, "保存失败");e.printStackTrace();}// 最后通知图库更新context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.fromFile(new File(file.getPath()))));}

代码比较简单,只需直接复制粘贴至项目,把相应的内容补齐。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。