Android——将图片加入到系统相册里面

Adnroid中保存图片的方法可能有如下两种:

  • 第一种是自己写方法,如下代码:
public static File saveImage(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();
    }
}

以上代码便是将Bitmap保存图片到指定的路径/sdcard/Boohee/下,文件名以当前系统时间命名,但是这种方法保存的图片没有加入到系统图库中

  • 第二种是调用系统提供的插入图库的方法:
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "title", "description");

调用以上系统自带的方法会把bitmap对象保存到系统图库中,但是这种方法无法指定保存的路径和名称,上述方法的title、description参数只是插入数据库中的字段,真实的图片名称系统会自动分配。

看似上述第二种方法就是我们要用到的方法,但是可惜的调用上述第二种插入图库的方法图片并没有立刻显示在图库中,而我们需要立刻更新系统图库以便让用户可以立刻查看到这张图片。

  • 更新系统图库的方法
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

上面那条广播是扫描整个sd卡的广播,如果你sd卡里面东西很多会扫描很久,在扫描当中我们是不能访问sd卡,所以这样子用户体现很不好,所以下面我们还有如下的方法:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File("/sdcard/Boohee/image.jpg"))););

或者还有如下方法:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File("/sdcard/Boohee/image.jpg"))););
final MediaScannerConnection msc = new MediaScannerConnection(mContext, new MediaScannerConnectionClient() {
    public void onMediaScannerConnected() {
        msc.scanFile("/sdcard/Boohee/image.jpg", "image/jpeg");
    }
    public void onScanCompleted(String path, Uri uri) {
        Log.v(TAG, "scan completed");
        msc.disconnect();
    }
});

上面代码的图片路径不管是通过自己写方法还是系统插入图库的方法都可以很容易的获取到。

  • 终极完美解决方案

那么到这里可能有人又会问了,如果我想把图片保存到指定的文件夹,同时又需要图片出现在图库里呢?答案是可以的,sdk还提供了这样一个方法:

MediaStore.Images.Media.insertImage(getContentResolver(), "image path", "title", "description");

  

上述方法的第二个参数是image path,这样的话就有思路了,首先自己写方法把图片指定到指定的文件夹,然后调用上述方法把刚保存的图片路径传入进去,最后通知图库更新。

所以写了一个方法,完整的代码如下:

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://" + path)));
}

原博客地址:http://stormzhang.github.io/android/2014/07/24/android-save-image-to-gallery/

时间: 2024-08-08 21:59:37

Android——将图片加入到系统相册里面的相关文章

android 调用系统相机获取图片、调用系统相册获取图片,并对图片进行截取

打开系统相册获取图片并截取,代码相对简单 1 Intent intent = new Intent(Intent.ACTION_GET_CONTENT,null); 2 intent.setType("image/*"); 3 intent.putExtra("crop", "true"); 4 5 //WIDTH 和 HEIGHT指的是截取框的宽高比例,如设WIDTH = 1,HEIGHT = 1,截取框就为正方形 6 intent.putEx

android将应用中图片保存到系统相册并显示

我应用到的场景是程序中在视频通讯时截图,将截图保存到本地相册中 /*** @param bmp 获取的bitmap数据 * @param picName 自定义的图片名*/ public static void saveBmp2Gallery(Bitmap bmp, String picName) { String fileName = null; //系统相册目录 String galleryPath= Environment.getExternalStorageDirectory() + F

将图片保存到系统相册的两种方法

第一种:采用系统的api直接使用: ContentResolver cr = getContentResolver(); String url = MediaStore.Images.Media.insertImage(cr, bmp, String.valueOf(System.currentTimeMillis()), ""); 但是,这种方式必须得刷新图库: sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.par

图片保存到系统相册

目前,UIImage保存到系统相册主要有两种方式: 方式一:(就一句话) UIImageWriteToSavedPhotosAlbum(img,self, @selector(image:didFinishSavingWithError:contextInfo:),nil); 然后实现回调: - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInf

Android缓存图片,在系统图库却看不见。怎么做到的?答:新建“.nomedia”的文件即可。

有些特定软件会在缓存图片文件夹自动生成名为“.nomedia”的文件,屏蔽了其他软件的扫描.代表此文件夹里面的MP3.MP4.RMVB.JPEG等图片音频视频等媒体文件,所以系统图库看不到.但一些特殊软件如 快图.MX Player也可以在设置中取消辨认这些文件!

iOS开发之保存照片到系统相册(Photo Album)

iOS开发之保存照片到系统相册(Photo Album) 保存照片到系统相册这个功能很多社交类的APP都有的,今天我们简单讲解一下,如何将图片保存到系统相册(Photo Album). 创建UIImageView 创建UIImageView是为了将照片展示出来,我们是要把UIImage保存到系统相册(Photo Album): #define SCREEN [UIScreen mainScreen].bounds.size self.image = [UIImage imageNamed:@"i

iOS开发>学无止境 - 保存照片到系统相册(Photo Album)

保存照片到系统相册这个功能很多社交类的APP都有的,今天我们简单讲解一下,如何将图片保存到系统相册(Photo Album). 创建UIImageView 创建UIImageView是为了将照片展示出来,我们是要把UIImage保存到系统相册(Photo Album): #define SCREEN [UIScreen mainScreen].bounds.size self.image = [UIImage imageNamed:@"iOSDevTip"]; UIImageView

MUI使用H5+Api调取系统相册多图选择及转base64码

伟大的哲学家曾说过"写代码,一定要翻文档" 这次我们需要用到的是调取系统相册进行多图上传,先奉上html5+api关于系统相册的文档链接链接:HTML5+ API Reference & gallery 首先一点,我们在使用5+Api前都需要在manifest.json文件中进行功能模块的添加,当然用Hbuilder的话大部分模块都已在内,这里是关于相册的模块 { // ... "permissions":{ // ... "Gallery&quo

Android拍照调用系统相册仿微信封装总结,治疗各种崩溃,图片横竖问题压缩等问题。

项目下载地址:https://github.com/Aiushtha/android-PictureSelector 最早使用android调用系统拍照然后遇到很多空指针等问题 以及各种android 不同版本Intent取data有时候会空指针之类的api兼容问题 像使用红米note在开了很多应用后,再启动拍照系统,会发生拍照崩溃图片丢失等问题 用微信控件有时拍照有极小概率拍照无效等等奇怪的问题 其原因是因为Activity被回收了,变量变成null, 参考下面一篇博客 http://blog