图片存储+添加到媒体库

来源:互联网 发布:凡科建站 源码 编辑:程序博客网 时间:2024/06/07 02:02
public static File saveImage(Bitmap bmp) {//创建一个存放图片的文件夹BooheeFile appDir = new File(Environment.getExternalStorageDirectory(), "Boohee");if (!appDir.exists()) {appDir.mkdir();}//图片名字String fileName = System.currentTimeMillis() + "herjk.jpg";File file = new File(appDir, fileName);try {FileOutputStream fos = new FileOutputStream(file);bmp.compress(CompressFormat.JPEG, 100, fos);fos.flush();fos.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}// 其次把文件插入到系统图库try {MediaStore.Images.Media.insertImage(mcontext.getContentResolver(), file.getAbsolutePath(), fileName, null);} catch (FileNotFoundException e) {e.printStackTrace();}// 最后通知图库更新mcontext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+ path)));return file;}

1 0