机器人 - 保存图片到相册 [英] android - save image into gallery

查看:267
本文介绍了机器人 - 保存图片到相册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像画廊的应用程序,我想用户可以把它存到了自己的画廊。 我创建了一个选项菜单用一个声音保存,以允许,但问题是...我怎么能保存图像到画廊?

这是我的code:

  @覆盖
        公共布尔onOptionsItemSelected(菜单项项){
            //处理项目选择
            开关(item.getItemId()){
            案例R.id.menuFinale:

                imgView.setDrawingCacheEnabled(真正的);
                点阵位图= imgView.getDrawingCache();
                文件根= Environment.getExternalStorageDirectory();
                档案文件=新的文件(root.getAbsolutePath()+/ DCIM /摄像头/ img.jpg);
                尝试
                {
                    file.createNewFile();
                    FileOutputStream中的ostream =新的FileOutputStream(文件);
                    bitmap.com preSS(比较pressFormat.JPEG,100,ostream的);
                    ostream.close();
                }
                赶上(例外五)
                {
                    e.printStackTrace();
                }



                返回true;
            默认:
                返回super.onOptionsItemSelected(项目);
            }
        }
 

我不知道这部分的code的:

 文件根= Environment.getExternalStorageDirectory();
                档案文件=新的文件(root.getAbsolutePath()+/ DCIM /摄像头/ img.jpg);
 

是正确的保存到画廊? 不幸的是,code不工作:(

解决方案

  MediaStore.Images.Media.insertImage(getContentResolver(),yourBitmap,yourTitle,yourDescription);
 

,前者code将添加图像在走廊尽头。如果你要那么它出现在年初或任何其他元数据修改的日期,请参见下面的code(SK的Cortesy,的 samkirton 的):

<一个href="https://gist.github.com/samkirton/0242ba81d7ca00b475b9">https://gist.github.com/samkirton/0242ba81d7ca00b475b9

  / **
 *机器人内部已被修改以存储在媒体文件夹的图像与
 *正确的日期元数据
 * @author samuelkirton
 * /
公共类CapturePhotoUtils {

    / **
     *在Android内部insertImage方法的一个副本,这个方法填充
     *与DATE_ADDED和DATE_TAKEN元数据。这解决一个共同的问题,即媒体
     *已插入手动获取保存在画廊的结束(因为不填充日期)。
     * @see android.provider.MediaStore.Images.Media#insertImage(ContentResolver的,位图,字符串,字符串)
     * /
    公共静态最后弦乐insertImage(ContentResolver的CR,
            位图源,
            串题,
            字符串描述){

        ContentValues​​值=新ContentValues​​();
        values​​.put(Images.Media.TITLE,职称);
        values​​.put(Images.Media.DISPLAY_NAME,职称);
        values​​.put(Images.Media.DESCRIPTION,说明);
        values​​.put(Images.Media.MIME_TYPE,为image / jpeg);
        //添加日期的元数据,以确保图像添加在画廊的前面
        values​​.put(Images.Media.DATE_ADDED,System.currentTimeMillis的());
        values​​.put(Images.Media.DATE_TAKEN,System.currentTimeMillis的());

        乌里URL = NULL;
        字符串stringUrl = NULL; / *要返回值* /

        尝试 {
            URL = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,价值观);

            如果(来源!= NULL){
                OutputStream的imageOut = cr.openOutputStream(URL);
                尝试 {
                    source.com preSS(Bitmap.Com pressFormat.JPEG,50,imageOut);
                } 最后 {
                    imageOut.close();
                }

                长ID = ContentUris.parseId(URL);
                //等待,直到产生MINI_KIND缩略图。
                位图miniThumb = Images.Thumbnails.getThumbnail(CR,ID,Images.Thumbnails.MINI_KIND,NULL);
                //这是为了向后兼容。
                storeThumbnail(CR,miniThumb,ID,50F,50F,Images.Thumbnails.MICRO_KIND);
            } 其他 {
                cr.delete(URL,NULL,NULL);
                URL = NULL;
            }
        }赶上(例外五){
            如果(网址!= NULL){
                cr.delete(URL,NULL,NULL);
                URL = NULL;
            }
        }

        如果(网址!= NULL){
            stringUrl = url.toString();
        }

        返回stringUrl;
    }

    / **
     *在Android内部StoreThumbnail方法的副本,它与insertImage所用
     *填充android.provider.MediaStore.Images.Media#insertImage所有正确的
     *元数据。该StoreThumbnail方法是私有的,因此必须在此重复。
     * @see android.provider.MediaStore.Images.Media(StoreThumbnail私有方法)
     * /
    私有静态最后的位图storeThumbnail(
            ContentResolver的CR,
            位图源,
            长的ID,
            浮动幅度,
            浮球高度,
            INT那种){

        //创建矩阵,它的规模
        字模=新的Matrix();

        浮动的scaleX =宽/ source.getWidth();
        浮动的scaleY =身高/ source.getHeight();

        matrix.setScale(将scaleX,的scaleY);

        位图拇指= Bitmap.createBitmap(源,0,0,
            source.getWidth(),
            source.getHeight(),矩阵,
            真正
        );

        ContentValues​​值=新ContentValues​​(4);
        values​​.put(Images.Thumbnails.KIND,实物);
        values​​.put(Images.Thumbnails.IMAGE_ID,(int)的内径);
        values​​.put(Images.Thumbnails.HEIGHT,thumb.getHeight());
        values​​.put(Images.Thumbnails.WIDTH,thumb.getWidth());

        乌里URL = cr.insert(Images.Thumbnails.EXTERNAL_CONTENT_URI,价值观);

        尝试 {
            OutputStream的thumbOut = cr.openOutputStream(URL);
            thumb.com preSS(Bitmap.Com pressFormat.JPEG,100,thumbOut);
            thumbOut.close();
            返回大拇指;
        }赶上(FileNotFoundException异常前){
            返回null;
        }赶上(IOException异常前){
            返回null;
        }
    }
}
 

i have an app with a gallery of images and i want that the user can save it into his own gallery. I've created an option menu with a single voice "save" to allow that but the problem is...how can i save the image into the gallery?

this is my code:

@Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle item selection
            switch (item.getItemId()) {
            case R.id.menuFinale:

                imgView.setDrawingCacheEnabled(true);
                Bitmap bitmap = imgView.getDrawingCache();
                File root = Environment.getExternalStorageDirectory();
                File file = new File(root.getAbsolutePath()+"/DCIM/Camera/img.jpg");
                try 
                {
                    file.createNewFile();
                    FileOutputStream ostream = new FileOutputStream(file);
                    bitmap.compress(CompressFormat.JPEG, 100, ostream);
                    ostream.close();
                } 
                catch (Exception e) 
                {
                    e.printStackTrace();
                }



                return true;
            default:
                return super.onOptionsItemSelected(item);
            }
        }

i'm not sure of this part of code:

File root = Environment.getExternalStorageDirectory();
                File file = new File(root.getAbsolutePath()+"/DCIM/Camera/img.jpg");

is it correct to save into the gallery? unfortunately the code doesn't work :(

解决方案

MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);

The former code will add the image at the end of the gallery. If you want to modify the date so it appears at the beginning or any other metadata, see the code below (Cortesy of S-K, samkirton):

https://gist.github.com/samkirton/0242ba81d7ca00b475b9

/**
 * Android internals have been modified to store images in the media folder with 
 * the correct date meta data
 * @author samuelkirton
 */
public class CapturePhotoUtils {

    /**
     * A copy of the Android internals  insertImage method, this method populates the 
     * meta data with DATE_ADDED and DATE_TAKEN. This fixes a common problem where media 
     * that is inserted manually gets saved at the end of the gallery (because date is not populated).
     * @see android.provider.MediaStore.Images.Media#insertImage(ContentResolver, Bitmap, String, String)
     */
    public static final String insertImage(ContentResolver cr, 
            Bitmap source, 
            String title, 
            String description) {

        ContentValues values = new ContentValues();
        values.put(Images.Media.TITLE, title);
        values.put(Images.Media.DISPLAY_NAME, title);
        values.put(Images.Media.DESCRIPTION, description);
        values.put(Images.Media.MIME_TYPE, "image/jpeg");
        // Add the date meta data to ensure the image is added at the front of the gallery
        values.put(Images.Media.DATE_ADDED, System.currentTimeMillis());
        values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());

        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 = Images.Thumbnails.getThumbnail(cr, id, Images.Thumbnails.MINI_KIND, null);
                // This is for backward compatibility.
                storeThumbnail(cr, miniThumb, id, 50F, 50F,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;
    }

    /**
     * A copy of the Android internals StoreThumbnail method, it used with the insertImage to
     * populate the android.provider.MediaStore.Images.Media#insertImage with all the correct
     * meta data. The StoreThumbnail method is private so it must be duplicated here.
     * @see android.provider.MediaStore.Images.Media (StoreThumbnail private method)
     */
    private static final 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(Images.Thumbnails.KIND,kind);
        values.put(Images.Thumbnails.IMAGE_ID,(int)id);
        values.put(Images.Thumbnails.HEIGHT,thumb.getHeight());
        values.put(Images.Thumbnails.WIDTH,thumb.getWidth());

        Uri url = cr.insert(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;
        }
    }
}

这篇关于机器人 - 保存图片到相册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆