Android保存图片到系统图库

http://stormzhang.com/android/2014/07/24/android-save-image-to-gallery/

最近有些用户反映保存图片之后在系统图库找不到保存的图片,遂决定彻底查看并解决下。

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

  • 第一种是自己写方法,如下代码:
public static void 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"))););
或者还有如下方法:

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)));
}

在实际的开发中发现在Android版本高于4.4的时候,还是不能看到图片,需要添加一下的判断:


Savepath:是保存的文件的位置

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//如果是4.4及以上版本 Intent mediaScanIntent = new Intent( Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); File file = new File(Savepath); Uri contentUri = Uri.fromFile(file); //out is your output file mediaScanIntent.setData(contentUri); HTWebViewActivity.this.sendBroadcast(mediaScanIntent); } else { sendBroadcast(new Intent( Intent.ACTION_MEDIA_MOUNTED, Uri.parse(Savepath))); }







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android Studio中保存图片到相册可以通过以下步骤进行: 1. 首先,确保你的应用已经申请了写入外部存储的权限。在AndroidManifest.xml文件中添加以下权限: ```xml <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ``` 2. 创建一个用于保存图片的方法,如下所示: ```java private void saveImageToGallery(Bitmap bitmap) { // 获取存储路径 String savePath = Environment.getExternalStorageDirectory().getPath() + "/YourAppName/"; // 创建文件夹 File appDir = new File(savePath); if (!appDir.exists()) { appDir.mkdirs(); } // 生成文件名 String fileName = System.currentTimeMillis() + ".jpg"; // 创建文件对象 File file = new File(appDir, fileName); try { FileOutputStream fos = new FileOutputStream(file); // 将bitmap保存到文件中 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } // 发送广播通知系统图库更新 Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri uri = Uri.fromFile(file); intent.setData(uri); sendBroadcast(intent); } ``` 3. 调用该方法来保存图片到相册: ```java Bitmap bitmap = ...; // 你要保存的图片Bitmap对象 saveImageToGallery(bitmap); ``` 这样就能将图片保存到相册中了。请注意,Android 10及以上版本需要额外处理,以适配分区存储的限制。这里提供的代码示例适用于Android 10以下的版本。如需适配Android 10及以上版本,请参考官方文档并进行相应修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值