android如何将生成的图片保存至手机相册并显示出来
16lz
2021-01-26
生成图片后,保存到指定位置,或者直接保存到相册,但是保存后相册不能直接看到,这时就需要同时通知系统图库更新
//保存文件到指定路径 public void saveMyBitmap(Context context,Bitmap bitmap) { String sdCardDir=Environment.getExternalStorageDirectory()+"/DCIM/"; File appDir =new File(sdCardDir, "HappyBirthday"); if (!appDir.exists()) { appDir.mkdir(); } String fileName = "HappyBirthday"+System.currentTimeMillis() + ".jpg"; File f = new File(appDir,fileName); try { FileOutputStream fos = new FileOutputStream(f); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
// 通知图库更新 Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri uri = Uri.fromFile(f); intent.setData(uri); context.sendBroadcast(intent);
当然方法不止这一种,但是在实际运行中,这个是最有效的。
更多相关文章
- android fih-mms的实现
- android 异常情况下的onSaveInatanceState()与onRestoreIntanceS
- Android(安卓)Jetpack 组件之 ViewModel(Kotlin)
- schema唤起指定页面
- android listview去除拉至顶部或底部时的蓝色
- android保存的wifi密码是明文……
- Android拍照获得图片URI为空的处理方法
- android 定义 程序 Scheme 接收特定URI开启Activity
- Win10下编译Android(安卓)Ogre3d 1.12.6