android保存图片到图库
16lz
2022-04-17
publicstaticvoidsaveImageToGallery(Contextcontext,Bitmapbmp){//首先保存图片FileappDir=newFile(Environment.getExternalStorageDirectory(),"Boohee");if(!appDir.exists()){appDir.mkdir();}StringfileName=System.currentTimeMillis()+".jpg";Filefile=newFile(appDir,fileName);try{FileOutputStreamfos=newFileOutputStream(file);bmp.compress(CompressFormat.JPEG,100,fos);fos.flush();fos.close();}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}//其次把文件插入到系统图库try{MediaStore.Images.Media.insertImage(context.getContentResolver(),file.getAbsolutePath(),fileName,null);}catch(FileNotFoundExceptione){e.printStackTrace();}//最后通知图库更新context.sendBroadcast(newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.parse("file://"+path)));}
更多相关文章
- Android(安卓)SQLite数据库的添 删 查 改
- 整理出15个Android很有用的代码片段
- Android开发之InstanceState详解
- Android(安卓)简单联系人操作
- android批量插入数据效率对比
- Android特有的数据结构分析
- Android深入浅出系列课程---Lesson6 AAF110503_Activity状态保存
- Android(安卓)手绘 - 支持保存为图片
- android 数据存储