Android的Media就有保存图片到相册的方法
MediaStore.Images.Media.insertImage(ContentResolver cr, Bitmap source, String title, String description)

但在使用的过程中发现,这个方法保存的图片是没有时间信息的,导致在查看相册的时候也不能正常的按时间排序的顺序显示,

查看insertImage的源码,发现方法只写了三个参数

values.put(Images.Media.TITLE, title);

values.put(Images.Media.DESCRIPTION, description);

values.put(Images.Media.MIME_TYPE, "image/jpeg");

所以这里按源码重新写了一个保存图片的方法

    /**
     * Insert an image and create a thumbnail for it.
     *
     * @param cr The content resolver to use
     * @param imagePath The path to the image to insert
     * @param name The name of the image
     * @param description The description of the image
     * @return The URL to the newly created image
     * @throws FileNotFoundException
     */
    public static String insertImage(ContentResolver cr, String imagePath,
                                     String name, String description) throws FileNotFoundException {
        // Check if file exists with a FileInputStream
        FileInputStream stream = new FileInputStream(imagePath);
        try {
            Bitmap bm = BitmapFactory.decodeFile(imagePath);
            String ret = insertImage(cr, bm, name, description);
            bm.recycle();
            return ret;
        } finally {
            try {
                stream.close();
            } catch (IOException e) {
            }
        }
    }


    /**
     * Insert an image and create a thumbnail for it.
     *  jliu : 对源码作过修改,加入了时间信息,默认插入时间为当前时间
     * @param cr The content resolver to use
     * @param source The stream to use for the image
     * @param title The name of the image
     * @param description The description of the image
     * @return The URL to the newly created image, or null if the image failed to be stored
     *              for any reason.
     */
    public static String insertImage(ContentResolver cr, Bitmap source,
                                     String title, String description) {
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, title);
        values.put(MediaStore.Images.Media.DESCRIPTION, description);
        values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis()); // 添加时间信息
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
        Uri url = null;
        String stringUrl = null;    /* value to be returned */
        try {
            url = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);//
            if (source != null) {
                OutputStream imageOut = cr.openOutputStream(url);
                try {
                    source.compress(Bitmap.CompressFormat.JPEG, 50, imageOut);
                } finally {
                    imageOut.close();
                }
                long id = ContentUris.parseId(url);
                // Wait until MINI_KIND thumbnail is generated.
                Bitmap miniThumb = MediaStore.Images.Thumbnails.getThumbnail(cr, id,
                        MediaStore.Images.Thumbnails.MINI_KIND, null);
                // This is for backward compatibility.
                StoreThumbnail(cr, miniThumb, id, 50F, 50F,
                        MediaStore.Images.Thumbnails.MICRO_KIND);
            } else {
                cr.delete(url, null, null);
                url = null;
            }
        } catch (Exception e) {
            if (url != null) {
                cr.delete(url, null, null);
                url = null;
            }
        }
        if (url != null) {
            stringUrl = url.toString();
        }
        return stringUrl;

    }

/**

* 生成预览图信息

*/

    private static Bitmap StoreThumbnail(
            ContentResolver cr,
            Bitmap source,
            long id,
            float width, float height,
            int kind) {
        // create the matrix to scale it
        Matrix matrix = new Matrix();

        float scaleX = width / source.getWidth();
        float scaleY = height / source.getHeight();
        matrix.setScale(scaleX, scaleY);
        Bitmap thumb = Bitmap.createBitmap(source, 0, 0,
                source.getWidth(),
                source.getHeight(), matrix,
                true);
        ContentValues values = new ContentValues(4);
        values.put(MediaStore.Images.Thumbnails.KIND,     kind);
        values.put(MediaStore.Images.Thumbnails.IMAGE_ID, (int)id);
        values.put(MediaStore.Images.Thumbnails.HEIGHT,   thumb.getHeight());
        values.put(MediaStore.Images.Thumbnails.WIDTH,    thumb.getWidth());
        Uri url = cr.insert(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, values);
        try {
            OutputStream thumbOut = cr.openOutputStream(url);
            thumb.compress(Bitmap.CompressFormat.JPEG, 100, thumbOut);
            thumbOut.close();
            return thumb;
        }
        catch (FileNotFoundException ex) {
            return null;
        }
        catch (IOException ex) {
            return null;
        }
    }

更多相关文章

  1. Android(安卓)InputMethod 源码分析,显示输入法流程
  2. Createing Dialogs/创建对话框
  3. Android(安卓)new Message()和Message.obtainMessage的区别
  4. android当前apn的状态以及获取方法
  5. Android自定义view绘制顺序及相关原理
  6. Android(安卓)init进程——解析配置文件
  7. Android(安卓)JNI 编程
  8. android 在源码中编译工程的方法
  9. Building Custom Components/建立自定义组件

随机推荐

  1. android中的选择器selector
  2. 浅谈Android中的ClassLoader
  3. AppBarLayout中android:fitsSystemWindow
  4. 修改Android手机的“虚拟机堆大小”和and
  5. Android核心模块及相关技术
  6. Android性能调优出发点
  7. [入门四]Android核心模块及相关技术
  8. Android(安卓)调试桥
  9. mac里边配置android开发环境,intellij开发
  10. Android中ScrollView无法正常achartengin