在实际使用过程中我们可能遇到将图片保存到系统相册的需求,如果做相机软件更是如此。今天看到别人的博客中有些到这个功能,就研究了一下。发现我们可以通过Media.insertImage()方法来进行,而且还可以将其他文件夹中的图片添加到系统相册中。但在研究中发现了个问题,很多人说添加后发现系统相册里一下子找不到,需要一会或者重启才能发现添加的图片,因此有了通知系统相册更新的方法。我的手机版本是4.4,使用添加的方法后立刻可以在手机相册的,相机文件夹中找到。这个……留着以后用到的话再详细测试吧。
先说下使用方式:
1.将bitmap保存到系统相册
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); /** * 调用以上系统自带的方法会把bitmap对象保存到系统图库中, * 但是这种方法无法指定保存的路径和名称 * 方法的title、description参数只是插入数据库中的字段,真实的图片名称系统会自动分配。 */ Media.insertImage(getContentResolver(), bitmap, "title", "描述文字");
这个title会在相册中图片的详细属性中看到,描述嘛可以写null,没啥意义。其实到最后系统会自己分配图片的名字的。
二、将别的文件夹中的图片插入到系统图库中
这里又涉及了通知系统图库更新的操作,这个手头没机子测试。所以暂时放在这
/** * 可以将保存在别的文件夹中的图片插入到系统图库中 * @param context * @param bmp */ public static void saveImageToGallery(Context context, Bitmap bmp) { // 首先保存图片 //图片的根目录 File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee"); if (!appDir.exists()) { appDir.mkdir(); } //图片名 String fileName = System.currentTimeMillis() + ".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(context.getContentResolver(), file.getAbsolutePath(), fileName, null); } catch (FileNotFoundException e) { e.printStackTrace(); } // 最后通知图库更新 context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + "Boohee"))); }
三、网上说的通知系统图库更新方法
/** * 通知系统相册更新的方法01 */ private void notif01() { //imageFile是图片的File对象; File imageFile = new File("/sdcard/Boohee/image.jpg"); Uri localUri = Uri.fromFile(imageFile); Intent localIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, localUri); sendBroadcast(localIntent); } /** * 通知系统相册更新的方法02 */ private void notif02() { File imageFile = new File("/sdcard/Boohee/image.jpg"); ContentValues localContentValues = new ContentValues(); localContentValues.put("_data", imageFile.toString()); localContentValues.put("description", "描述文字"); localContentValues.put("mime_type", "image/jpeg"); ContentResolver localContentResolver = getContentResolver(); Uri localUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; localContentResolver.insert(localUri, localContentValues); }
系统源码中保存图片的代码片段
Context context = params[0].context; Bitmap image = params[0].image; Resources r = context.getResources(); try { // Create screenshot directory if it doesn't exist mScreenshotDir.mkdirs(); // media provider uses seconds for DATE_MODIFIED and DATE_ADDED, but milliseconds // for DATE_TAKEN long dateSeconds = mImageTime / 1000; // Save the screenshot to the MediaStore ContentValues values = new ContentValues(); ContentResolver resolver = context.getContentResolver(); values.put(MediaStore.Images.ImageColumns.DATA, mImageFilePath); values.put(MediaStore.Images.ImageColumns.TITLE, mImageFileName); values.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, mImageFileName); values.put(MediaStore.Images.ImageColumns.DATE_TAKEN, mImageTime); values.put(MediaStore.Images.ImageColumns.DATE_ADDED, dateSeconds); values.put(MediaStore.Images.ImageColumns.DATE_MODIFIED, dateSeconds); values.put(MediaStore.Images.ImageColumns.MIME_TYPE, "image/png"); values.put(MediaStore.Images.ImageColumns.WIDTH, mImageWidth); values.put(MediaStore.Images.ImageColumns.HEIGHT, mImageHeight); Uri uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); OutputStream out = resolver.openOutputStream(uri); image.compress(Bitmap.CompressFormat.PNG, 100, out); out.flush(); out.close(); // update file size in the database values.clear(); values.put(MediaStore.Images.ImageColumns.SIZE, new File(mImageFilePath).length()); resolver.update(uri, values, null, null); } catch (Exception e) { }
参考自:
http://stormzhang.github.io/android/2014/07/24/android-save-image-to-gallery/
http://blog.csdn.net/xu_fu/article/details/39158747
http://www.open-open.com/lib/view/open1414226791465.html